What Is JavaScript?

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…

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>

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:

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!");

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 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

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.

What you will learn