You can already build a page with HTML and style it with CSS. Now try clicking the heading on your page: nothing happens. The page does not react, like a poster on a wall. Some pages can do more: a button that counts the items in your shopping cart, a form that warns you when you mistype your email, content that changes without reloading the page. All of that is the work of JavaScript.
JavaScript is a programming language that runs in the browser: it is made of instructions, and the browser runs them one by one. It also has a different job from HTML and CSS. HTML sets the page's content, CSS sets how it looks, and JavaScript sets how it behaves: what happens when a visitor clicks or types. Think of the page as a house. HTML is the frame, CSS is the paint, and JavaScript is the electricity: when you press the switch (a visitor clicks), a light comes on (the page changes).
JavaScript goes inside <script>
You can write JavaScript right in an HTML file, between the <script> and </script> tags:
<h1>Hello World!</h1>
<script>
console.log("Hello from JavaScript!");
</script>
<h1>is an ordinary heading, just like the ones you wrote in the HTML lessons.<script>and</script>are the HTML tags that hold JavaScript. The browser doesn't read what is inside them as page text, but as instructions.console.log("Hello from JavaScript!");is one JavaScript instruction. The next section explains what it means.- The semicolon
;marks the end of one instruction.
The page shows only the heading "Hello World!". The sentence "Hello from JavaScript!" doesn't appear on the page. console.log prints (writes) that sentence to another place, called the console. The next section explains it.
When there are several instructions, the browser runs them line by line, from top to bottom. JavaScript can also go in a file of its own. You then add that file to the page with <script src="app.js"></script>. The src attribute here works like src on <img>: it holds the file's address.
console.log: print a result to the console
The console is a panel in the browser made for developers (the people who write code). To open it, press F12, then choose the Console tab. If F12 opens nothing:
- Mac: press Fn+F12, or Cmd+Option+J to go straight to the Console in Chrome and Edge.
- Windows and Linux: Ctrl+Shift+J also goes straight to the Console in Chrome and Edge.
- Safari: first turn on Settings > Advanced > "Show features for web developers", then press Cmd+Option+C.
- Phone: phone browsers have no console. Use the output panel in the exercises.
In the exercises, the console is the output panel under the editor. Visitors to the page never see the console. So you use the console to check what your own code did. Here is one instruction that prints to the console:
console.log("Hello!");
console.logis a function: a ready-made instruction with a name. The job of this function is to print to the console.- The parentheses after
console.loghold what you want to print. Writing a function's name followed by parentheses, as in this line, is called calling the function (a function call). "Hello!"is text. In JavaScript, text always goes inside quote marks.
The console shows Hello!, without the quote marks. In the code, quote marks only mark which part is text.
Keep text in a variable
In this lesson's exercise, the code stores a piece of text in a variable first, then prints it:
let message = "Hello, JavaScript!";
console.log(message);
- The first line makes a variable: a container named
messagethat holds the text"Hello, JavaScript!".letmeans "make a new variable", and=means "fill it with". - The second line prints what
messageholds. Notice thatmessagehas no quote marks on the second line. Without quote marks, JavaScript readsmessageas the name of a variable, then takes what that variable holds.
The console shows Hello, JavaScript!. If you write console.log("message") with quote marks, it prints the word message, not what the variable holds. You learn more about them in the next lesson, Variables.
Capital and small letters are different
JavaScript is case-sensitive: a capital letter and a small letter are not the same letter. So console and Console are not the same name, and only console in small letters exists. That is why Console.log("Hello") fails. The console panel shows the error Console is not defined, which means the name Console isn't known, and the code stops at that line. Names you choose yourself work the same way: message and Message are not the same name.
Where JavaScript runs
JavaScript was made for the browser, and every browser can already run it. So you don't need to install anything. Today JavaScript also runs outside the browser, for example on a server. A server is a computer that stores a site and sends its pages to the browser. Outside the browser, a program called Node.js runs it. So one language works in two places: the browser and Node.js. Deno and Bun are two other programs that also run JavaScript outside the browser.
⚠️ What confuses people at the start
- Thinking
console.logshows something to visitors. The console is only for developers. To show something to visitors, JavaScript has to change the page's content. You'll learn how later, in the DOM Manipulation lesson. - Quote marks that don't pair up.
"Hellowith no closing quote mark causes an error. Open and close text with the same kind of quote mark:"Hello"(double quotes) or'Hello'(single quotes). - Thinking JavaScript is Java. The names look alike, but JavaScript and Java are two different languages.
TL;DR: JavaScript is a programming language: it is made of instructions that the browser runs one by one. HTML sets the page's content, CSS how it looks, JavaScript how it behaves. JavaScript code goes inside the <script> tag and runs from top to bottom. console.log(...) prints to the console, a panel that only developers (people who write code) see. Text goes inside quote marks. The name of a variable (a named container that holds text or another value) has no quote marks. Capital and small letters are not the same: write console, not Console. JavaScript runs in the browser with nothing to install, and outside the browser through a program called Node.js.
⚡ JavaScript goes inside <script>
Write:
<h1>Hello World!</h1>
<script>
console.log("Hello from JavaScript!");
</script>
Guess first: does the sentence Hello from JavaScript! appear on the page?
No. The page shows only the heading Hello World!. console.log prints that sentence to the console, a panel in the browser that only developers (the people who write code) see.
Meaning: JavaScript is made of instructions for the browser, and it goes inside the HTML tag <script>. The browser reads what is inside <script> as instructions, not as page text. The browser runs those instructions line by line, from top to bottom. The semicolon ; marks the end of one instruction. The instruction console.log(...) prints to the console, not to the page.
⚡ Text gets quote marks, a variable's name doesn't
Write:
let message = "Hello, JavaScript!";
console.log(message);
Guess first: what gets printed if you write console.log("message")?
The word message. With quote marks, JavaScript reads message as plain text and prints it as written. Without quote marks, as in the code above, JavaScript reads message as the name of a variable and prints what it holds: Hello, JavaScript!.
Meaning: A variable is a named container that holds a value, here a piece of text. let makes a new variable named message, and = fills it. console.log(message) prints what message holds. Text always goes inside quote marks, a variable's name doesn't.
⚡ console, not Console
Meaning: JavaScript is case-sensitive: a capital letter and a small letter are not the same. Only console in small letters exists. So Console.log("Hello") fails with the error Console is not defined (the name Console isn't known), and the code stops at that line. Names you choose yourself work the same way: message and Message are not the same name.