Introduction to JavaScript
The language of the web
Skills taught: js.basics, js.script-tag, js.console
JavaScript is the programming language that makes web pages interactive. While HTML describes structure and CSS describes appearance, JavaScript adds behaviour.
Adding JavaScript to a page
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>My Page</title>
</head>
<body>
<h1>Hello!</h1>
<button id="btn">Click me</button>
<!-- External file (recommended) -->
<script src="app.js" defer></script>
<!-- Or inline (for small snippets) -->
<script>
console.log('JavaScript is running!');
</script>
</body>
</html>
Tip: Always place <script> tags at the bottom of <body> or use the defer attribute on external scripts. This ensures the HTML is fully parsed before your JS runs.
Your first JavaScript
// This is a comment — the browser ignores it
console.log('Hello, world!'); // prints to browser DevTools console
// Alert the user (avoid overusing — it blocks the page)
alert('Welcome!');
// Respond to a button click
const btn = document.getElementById('btn');
btn.addEventListener('click', function () {
console.log('Button was clicked!');
});
The browser console
The browser's developer console (F12 → Console tab) is your best friend. Use console.log() to inspect values while writing code:
console.log('Text:', 'Hello'); // Text: Hello
console.log('Number:', 42); // Number: 42
console.log('Array:', [1, 2, 3]); // Array: [1, 2, 3]
console.log('Object:', { a: 1 }); // Object: {a: 1}
console.warn('This is a warning'); // yellow
console.error('This is an error'); // red
console.table([{ name: 'HTML', year: 1993 }, { name: 'JS', year: 1995 }]);
JavaScript is not Java
Despite the name, JavaScript and Java are completely different languages. JavaScript was named after Java for marketing reasons in 1995. They share nothing except the name.
Frequently Asked Questions
Do I need to install anything to run JavaScript?
No. Every modern browser includes a JavaScript engine. Open DevTools (F12), go to the Console tab, and you can run JavaScript immediately. For Node.js (server-side JS), you install the Node runtime separately.
Where should I put the <script> tag?
At the bottom of <body> or in <head> with the defer attribute. This ensures the HTML is loaded before your script tries to access elements. Scripts in <head> without defer block page rendering.
What is the difference between JavaScript and TypeScript?
TypeScript is a superset of JavaScript that adds static type annotations. TypeScript code is compiled to JavaScript before running in the browser. Learn JavaScript first — TypeScript is learned on top of it.