Why JavaScript?
Meet the language that makes web pages come alive: what JavaScript does, where it runs, why it’s a friendly first language, and why it has almost nothing to do with Java.
- 01The web’s three layersSwitch HTML, CSS, and JavaScript on and off.
- 02JavaScript everywhereSort real apps by where their JavaScript runs.
- 03Your first programStep through five lines and predict the output.
What is JavaScript?
JavaScript is a programming language: a way to write step-by-step instructions that a computer follows. What makes it special is where it runs. Every web browser, including Chrome, Safari, Firefox, and Edge, has JavaScript built in. That makes it the one language that can make any web page react to you.
Whenever a website does something without loading a whole new page, JavaScript is usually behind it:
- A Like count that goes up the moment you tap it.
- A menu that slides open, or a photo gallery you can swipe.
- A form that warns “That email looks wrong” before you press Send.
- A map you can drag around, or a game that runs right in a tab.
HTML puts things on the page, CSS makes them look good, and JavaScript makes them do things.
A recipe is a list of steps, in order: crack the eggs, whisk them, heat the pan. The cook follows each step exactly, from top to bottom. A program is the same idea: a list of instructions for a computer. JavaScript is the language the recipe is written in, and every browser is a cook that reads it fluently.
- In real life: The recipe card
- In JavaScript: Your program: a file of JavaScript
- In real life: One step, like “crack two eggs”
- In JavaScript: One statement, like
let likes = 0; - In real life: A cook who follows the steps in order
- In JavaScript: The browser’s JavaScript engine
- In real life: The finished dish
- In JavaScript: What you see: a changed page, or a message in the console
Where the analogy stops: A human cook can guess what you meant. A computer can’t: a typo like consle.log instead of console.log stops the program with an error instead of being quietly fixed.
In this lesson you’ll switch a web page’s layers on and off, step through your first five lines of real code, sort real apps by where their JavaScript runs, and find out why “JavaScript” has almost nothing to do with “Java.”
The language of the web: HTML, CSS & JavaScript
INTERACTIVEA web page is usually built from three languages, and each one has a single job:
- HTML (HyperText Markup Language) describes the content: headings, paragraphs, images, and buttons.
- CSS (Cascading Style Sheets) describes the looks: colors, sizes, spacing, and layout.
- JavaScript describes the behavior: what happens when you click, type, scroll, or wait.
Building a house starts with walls, rooms, and doors. Then come paint and furniture to make it pleasant. Finally, electricity makes it work: flip a switch, and a light turns on. A house without wiring still stands. It just doesn’t respond to you.
- In real life: Walls, rooms, and doors
- In JavaScript: HTML: the page’s structure and content
- In real life: Paint, curtains, and furniture
- In JavaScript: CSS: colors, spacing, and layout
- In real life: Wiring: flip a switch and a light comes on
- In JavaScript: JavaScript: click a button and something happens
Where the analogy stops: A house needs walls before paint, but a web page’s layers are separate pieces the browser combines. A page can run JavaScript with no CSS at all; try it below.
Here’s a tiny page with a Like button. Its HTML is always on. Switch the CSS and JavaScript layers on and off, click Like each time, and watch what changes.
<div class="card"> <p>Learning JavaScript today!</p> <button id="like">♥ Like</button> <span id="count">0 likes</span></div>.card { padding: 16px; border: 1px solid silver; border-radius: 12px;}#like { padding: 6px 14px; border: none; border-radius: 999px; background: royalblue; color: white; font-weight: bold;}const button = document.querySelector("#like");const count = document.querySelector("#count");let likes = 0; button.addEventListener("click", () => { likes = likes + 1; count.textContent = likes + " likes";});Learning JavaScript today!
0 likesOnly the HTML is on: the text and button appear, unstyled. Click Like: does anything happen? Then switch on CSS and JavaScript, one at a time.
What did you notice?
- HTML alone already shows the text and the button. It just can’t do anything when you click.
- CSS changes only the looks. A pretty button still does nothing on its own.
- JavaScript adds behavior: it listens for clicks and updates the count, even when CSS is off.
Now read the JavaScript layer in plain words. Lines 1 and 2 find the button and the count text on the page. Line 3 creates likes, a named value that starts at 0. Line 5 tells the browser, “when the button is clicked, run the code inside.” Line 6 adds one to likes, and line 7 writes the new number onto the page. (Yes, after one click it says “1 likes”. Fixing that needs an if, which you’ll learn in the Control flow module.)
You don’t need to understand every symbol yet; each one gets its own lesson. Remember the shape instead: find something on the page, listen for an event, change the page. An enormous amount of front-end code follows that pattern.
Your first taste: five lines of JavaScript
STEP THROUGHReal programs are made of small steps like the ones behind that Like button. Here is a program that counts likes without any web page at all. First, meet the pieces:
let likes = 0;creates a variable: a name,likes, that holds a value, 0. Think of a jar with a label on it.=means “store the value on the right in the name on the left.” It is not the equals sign from math class.+adds numbers, and glues pieces of text together.const messagecreates another variable, one that won’t be given a new value later.console.log(...)prints a value in the console, a panel in your browser where code can leave notes for developers.
Predict what line 5 prints. Then click the blue line, or press Step, and follow the program one event at a time. Afterward, change how many likes lines 2 and 3 add, and predict again.
Predict what line 5 prints. Then click the blue line, or press Step, to run one small event at a time.
script
likes = likes + 1;likes = likes + 1;const message = "Liked " + likes + " times";console.log(message);In math, likes = likes + 1 would be impossible: nothing equals itself plus one. In JavaScript it’s an instruction, read from the right: work out likes + 1, then store the answer in likes. Say it as “likes becomes likes plus one.”
Notice that the computer did exactly what the lines said, in order, and nothing more. That predictability is what makes programming learnable: if you can trace the steps, you can predict the result.
Front end, back end, mobile & desktop
JavaScript was born in the browser in 1995. Today it runs almost anywhere a computer does:
| Where | What JavaScript does there | Real examples |
|---|---|---|
| In the browser (front end) | Makes web pages interactive | Menus, forms, maps, and web apps like Gmail and Google Docs |
| On a server (back end) | Runs with Node.js, Deno, or Bun on computers in data centers | Saving data, checking logins, sending emails |
| In a phone app | Builds iPhone and Android apps with React Native or Ionic | One codebase for both phone platforms |
| In a desktop app | Builds computer programs with Electron | Visual Studio Code, Slack, and Discord |
In a restaurant, you sit in the dining room, read the menu, and order. You never see the kitchen, where the food is actually made, or the pantry, where ingredients are stored. Websites are split the same way.
- In real life: The dining room: menus, tables, and your waiter
- In JavaScript: The front end: the web page in your browser
- In real life: The kitchen, where orders are cooked
- In JavaScript: The back end: a server that handles requests
- In real life: The pantry and the fridge
- In JavaScript: A database that stores information
- In real life: The waiter carrying orders between rooms
- In JavaScript: Network requests between browser and server
Where the analogy stops: A restaurant needs different people for the dining room and the kitchen. With JavaScript, one language can work in both rooms; developers who do both are called full stack.
Put it to the test. Where does the JavaScript in each example run?
- A search box that suggests results as you type on a website
- Checking a password against the accounts stored in a database
- An iPhone and Android app written once with React Native
- Visual Studio Code, the code editor you install
- Warning that an email field is empty before a form is sent
- Sending you a password-reset email
- A phone app built with Ionic, which wraps web code in an app
- The Slack app you install on your laptop
Choose where each example’s JavaScript runs. Every answer explains itself, so a wrong guess still teaches you something.
JavaScript even automates other software. Google Apps Script, which adds custom features to Google Sheets and Docs, is JavaScript.
What makes it a friendly first language
Plenty of languages can build great software. JavaScript has a few things going for it as a first language:
- Nothing to install. Your browser already runs JavaScript. Open its console and start typing; Exercise 1 shows you how.
- Instant feedback. Change a line and see the result in seconds, just like the experiments on this page.
- Flexible. You don’t have to declare what kind of value a variable holds; JavaScript works it out as the program runs. This is called dynamic typing.
- Useful everywhere. The same language builds web pages, servers, and apps, so everything you learn keeps paying off.
- A huge community. Millions of developers, free references like MDN, and millions of open-source packages mean someone has usually solved your problem before.
That flexibility has a price: JavaScript sometimes converts values for you when you didn’t ask it to. Predict each result below, then step through to check. Two are easy; two are JavaScript being a little too helpful.
Predict each result before you step. Two are easy, and two are JavaScript being a little too helpful.
script
"Hello, " + "world";"5" + 3;"5" - 3;"5" + 3 gives "53" because the quotes make "5" text, and + joins text. The Type conversion lesson explains exactly when JavaScript converts values, and how to convert them on purpose so there are no surprises.
JavaScript is not Java
Despite the name, JavaScript and Java are different languages, made by different people, for different jobs. In 1995, Netscape wanted a quick scripting language for its web browser. Brendan Eich built the first version in about ten days. It was called Mocha, then LiveScript, and was finally renamed JavaScript, mostly as marketing: Java was the exciting new language of the day, and Netscape had a deal with Java’s maker, Sun Microsystems.
Programmers love this old joke. “Car” and “carpet” share their first three letters, but you can’t drive a carpet to work. Java and JavaScript share their first four letters, and not much else.
Where the analogy stops: The two languages do share some punctuation borrowed from the C language, like curly braces, so short snippets can look alike. Look for clues such as System.out.println (Java) or console.log (JavaScript).
| Java | JavaScript | |
|---|---|---|
| Created by | James Gosling’s team at Sun Microsystems, 1995 | Brendan Eich at Netscape, 1995 |
| Main home | Servers, Android apps, and large business systems | Web browsers, plus servers and apps |
| Types | Declared up front: int score = 10; | Worked out while running: let score = 10; |
| How it runs | Compiled to bytecode first, then run by the Java Virtual Machine | Run from source code by an engine inside a browser or runtime |
| Printing a message | System.out.println("Hi"); | console.log("Hi"); |
Can you tell them apart? Sort each line of code by language. Look for the clues in the table.
System.out.println("Hello");console.log("Hello");int score = 10;let score = 10;public static void main(String[] args) {document.querySelector("#like");String name = "Ada";const greet = () => "Hi";
Pick the language each line is written in. Type names like int and String are a strong clue.
Common misconceptions
“JavaScript only runs in web browsers.”
It started there, but Node.js, Deno, and Bun run it on servers and laptops, and tools like React Native and Electron turn it into phone and desktop apps. You sorted eight real examples above.
“JavaScript is a simpler version of Java.”
They’re unrelated languages with similar names. The “Java” in JavaScript was a 1995 marketing decision, not a family tie.
“HTML is a programming language, so I already code.”
HTML is a markup language: it describes content, but on its own it can’t make decisions, repeat steps, or react to a click. JavaScript can. Knowing HTML is still a great head start, because JavaScript spends much of its time changing HTML.
“JavaScript is a toy for small effects.”
It powers full applications like Gmail, Google Docs, and Visual Studio Code, plus countless servers. It has grown up a lot since its ten-day beginning.
“You need to learn another language first.”
No. JavaScript is one of the most popular first languages, and everything you need to start is already in your browser.
For more than a decade, JavaScript has topped the “most commonly used language” list in Stack Overflow’s yearly developer survey.
Practice: your first JavaScript
5 EXERCISESReading is good; doing is better. Each exercise takes a few minutes. Try it before you open a hint, and open hints before the solution.
- Open your browser’s developer console. In Chrome or Edge, press
F12, orCtrl+Shift+J(Windows, Linux) orCmd+Option+J(Mac). In Firefox, useCtrl+Shift+KorCmd+Option+K. - Click next to the
>prompt, type2 + 2, and press Enter.
The console runs what you type and shows the result right away: 2 + 2 is 4. You just ran JavaScript! Try "Hello, " + "world" next.
Without running it, work out what this program prints. Then check your answer below.
let likes = 3;
likes = likes + 2;
likes = likes + 2;
console.log("Liked " + likes + " times");Line 1 stores 3. Line 2 makes it 5, and line 3 makes it 7. Line 4 joins the pieces, so the console prints Liked 7 times. Paste the code into your console to confirm it.
Using the Like button from the experiment, decide which layer you’d edit for each change:
- Make the Like button green.
- Add a second button that says Share.
- Make each click add 10 likes instead of 1.
/* 1. CSS: change the button's look */
#like { background: green; }
<!-- 2. HTML: add new content -->
<button id="share">Share</button>
// 3. JavaScript: change the behavior
likes = likes + 10;Colors are looks, so the first change is CSS. A new button is new content, so the second is HTML. What a click does is behavior, so the third is JavaScript: change likes + 1 to likes + 10.
- Open any website you like; a news site or an online store works well.
- Open the developer tools (
F12), choose the Network tab, and reload the page. - Click the JS filter to show only JavaScript files.
How many JavaScript files did the page load? More or fewer than you expected?
Most modern sites load several JavaScript files, and big web apps can load dozens or even hundreds. Each adds behavior: menus, video players, analytics, chat widgets. Every one of them runs on the same kind of engine you used in Exercise 1.
In your console, write a program that:
- creates a variable named
scoreholding 10, - adds 5 to it, and
- prints it with
console.log.
Tip: press Shift + Enter to start a new line in the console without running the code yet.
let score = 10;
score = score + 5;
console.log(score);let creates score with 10. Line 2 works out score + 5, which is 15, and stores it back. console.log(score) prints 15. If the console then shows undefined, that’s the value of the console.log call itself, not a mistake.
Quiz: check your understanding
7 QUESTIONSYour score counts first tries, and every choice explains itself, so read the explanations for wrong answers too.
Question 1 of 7Which layer of a web page makes a button do something when you click it?
Choose an answer to see the explanation.
Question 2 of 7What does this program print?
Read the code, then predictlet likes = 10; likes = likes + 5; console.log(likes);Choose an answer to see the explanation.
Question 3 of 7Where can JavaScript run today?
Choose an answer to see the explanation.
Question 4 of 7Why is JavaScript called JavaScript?
Choose an answer to see the explanation.
Question 5 of 7What does this line print?
Read the code, then predictconsole.log("5" + 3);Choose an answer to see the explanation.
Question 6 of 7Which statement about HTML is true?
Choose an answer to see the explanation.
Question 7 of 7What do you need to install before running your first line of JavaScript?
Choose an answer to see the explanation.
Key takeaways
- JavaScript is a programming language that every web browser understands, which makes it the language of interactive web pages.
- HTML is the content, CSS is the looks, and JavaScript is the behavior.
- It also runs on servers (Node.js, Deno, Bun), in phone apps, and in desktop apps.
- It’s a friendly first language: nothing to install and instant feedback. Its flexibility brings a few surprises, like
"5" + 3giving"53". - Despite the name, JavaScript is not Java.
Remember the one-liner.
HTML is what’s on the page. CSS is how it looks. JavaScript is what it does.
Up next: A short history of JavaScript, the story of how a ten-day prototype became the language of the web, and why a new version comes out every year.