cf.completefrontendCode editorOpen lab
THE JAVASCRIPT FIELD GUIDE

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.

Three things to take away
  • 01
    The web’s three layersSwitch HTML, CSS, and JavaScript on and off.
  • 02
    JavaScript everywhereSort real apps by where their JavaScript runs.
  • 03
    Your 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.
The short version

HTML puts things on the page, CSS makes them look good, and JavaScript makes them do things.

Real-life analogyA program is a recipe

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

INTERACTIVE

A 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.
Real-life analogyA web page is a house

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.

Switch the layers on and off
<div class="card">  <p>Learning JavaScript today!</p>  <button id="like">♥ Like</button>  <span id="count">0 likes</span></div>
CSS · switched offPop out in the code editor (opens in a new tab)CSS
.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;}
JavaScript · switched offPop out in the code editor (opens in a new tab)JavaScript
const button = document.querySelector("#like");const count = document.querySelector("#count");let likes = 0; button.addEventListener("click", () => {  likes = likes + 1;  count.textContent = likes + " likes";});
Live previewHTML

Learning JavaScript today!

0 likes
Try it yourself
Layers:

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

A live preview of the three layers. Switching JavaScript on adds the click behavior on lines 5–8. The code is simplified, and the preview uses this site’s theme colors.

What did you notice?

  1. HTML alone already shows the text and the button. It just can’t do anything when you click.
  2. CSS changes only the looks. A pretty button still does nothing on its own.
  3. 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 THROUGH

Real 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 message creates 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.

Step through your first program
Step 0 of 7Ready
Your turn: follow the blue line

Predict what line 5 prints. Then click the blue line, or press Step, to run one small event at a time.

Running in
  1. script
Next: line 1
Click the blue line to take the next stepPop out in the code editor (opens in a new tab)JavaScript
likes = likes + 1;likes = likes + 1;const message = "Liked " + likes + " times";console.log(message);
CallStoreChangeResultRun = next line. Ran = already executed.
Recent returnsNothing yet. Start with the blue line.
Change lines 2 and 3: how many likes does each one add?

Changing the lines starts a fresh run. Predict line 5 again before you step.

A guided replay recorded from real JavaScript calls, not an engine debugger. Step follows executed statements; Back reviews a snapshot. Reset starts a fresh run.
= is not math’s equals sign

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 JavaScript runs today
WhereWhat JavaScript does thereReal examples
In the browser (front end)Makes web pages interactiveMenus, 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 centersSaving data, checking logins, sending emails
In a phone appBuilds iPhone and Android apps with React Native or IonicOne codebase for both phone platforms
In a desktop appBuilds computer programs with ElectronVisual Studio Code, Slack, and Discord
Real-life analogyFront end and back end are a restaurant

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?

Where is this JavaScript running?
  • 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
Try it yourself
0 of 8 correct

Choose where each example’s JavaScript runs. Every answer explains itself, so a wrong guess still teaches you something.

Choose a category for every card. You can change an answer at any time; Reset clears them all.
Fun fact

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:

  1. Nothing to install. Your browser already runs JavaScript. Open its console and start typing; Exercise 1 shows you how.
  2. Instant feedback. Change a line and see the result in seconds, just like the experiments on this page.
  3. 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.
  4. Useful everywhere. The same language builds web pages, servers, and apps, so everything you learn keeps paying off.
  5. 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, then evaluate
Step 0 of 4Ready
Your turn: follow the blue line

Predict each result before you step. Two are easy, and two are JavaScript being a little too helpful.

Running in
  1. script
Next: line 1
Click the blue line to take the next stepPop out in the code editor (opens in a new tab)JavaScript
"Hello, " + "world";"5" + 3;"5" - 3;
CallStoreChangeResultRun = next line. Ran = already executed.
Recent returnsNothing yet. Start with the blue line.
A guided replay recorded from real JavaScript calls, not an engine debugger. Step follows executed statements; Back reviews a snapshot. Reset starts a fresh run.
Don’t memorize the surprises

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

Real-life analogyJava is to JavaScript as car is to carpet

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 and JavaScript at a glance
JavaJavaScript
Created byJames Gosling’s team at Sun Microsystems, 1995Brendan Eich at Netscape, 1995
Main homeServers, Android apps, and large business systemsWeb browsers, plus servers and apps
TypesDeclared up front: int score = 10;Worked out while running: let score = 10;
How it runsCompiled to bytecode first, then run by the Java Virtual MachineRun from source code by an engine inside a browser or runtime
Printing a messageSystem.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.

Java or JavaScript?
  • 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";
Try it yourself
0 of 8 correct

Pick the language each line is written in. Type names like int and String are a strong clue.

Choose a category for every card. You can change an answer at any time; Reset clears them all.

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.

Fun fact

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 EXERCISES

Reading is good; doing is better. Each exercise takes a few minutes. Try it before you open a hint, and open hints before the solution.

Exercise 1 · Warm-upWake up your browser’s console
  1. Open your browser’s developer console. In Chrome or Edge, press F12, or Ctrl + Shift + J (Windows, Linux) or Cmd + Option + J (Mac). In Firefox, use Ctrl + Shift + K or Cmd + Option + K.
  2. Click next to the > prompt, type 2 + 2, and press Enter.

Answer, then press Check. Spacing and letter case don’t matter.

    Exercise 2 · PracticePredict the likes

    Without running it, work out what this program prints. Then check your answer below.

    Starter codePop out in the code editor (opens in a new tab)JavaScript
    let likes = 3;
    likes = likes + 2;
    likes = likes + 2;
    console.log("Liked " + likes + " times");

    Answer, then press Check. Spacing and letter case don’t matter.

      Exercise 3 · PracticeWhich layer would you change?

      Using the Like button from the experiment, decide which layer you’d edit for each change:

      1. Make the Like button green.
      2. Add a second button that says Share.
      3. Make each click add 10 likes instead of 1.

      Answer, then press Check. Spacing and letter case don’t matter.

        Exercise 4 · ChallengeFind JavaScript in the wild
        1. Open any website you like; a news site or an online store works well.
        2. Open the developer tools (F12), choose the Network tab, and reload the page.
        3. Click the JS filter to show only JavaScript files.

        How many JavaScript files did the page load? More or fewer than you expected?

          Exercise 5 · ChallengeWrite your own three-line program

          In your console, write a program that:

          1. creates a variable named score holding 10,
          2. adds 5 to it, and
          3. prints it with console.log.

          Tip: press Shift + Enter to start a new line in the console without running the code yet.

          Answer, then press Check. Spacing and letter case don’t matter.

            Quiz: check your understanding

            7 QUESTIONS

            Your score counts first tries, and every choice explains itself, so read the explanations for wrong answers too.

            Lesson quiz · 7 questionsScore: first tries count
            1. 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.

            2. Question 2 of 7What does this program print?

              Read the code, then predictPop out in the code editor (opens in a new tab)JavaScript
              let likes = 10;
              likes = likes + 5;
              console.log(likes);

              Choose an answer to see the explanation.

            3. Question 3 of 7Where can JavaScript run today?

              Choose an answer to see the explanation.

            4. Question 4 of 7Why is JavaScript called JavaScript?

              Choose an answer to see the explanation.

            5. Question 5 of 7What does this line print?

              Read the code, then predictPop out in the code editor (opens in a new tab)JavaScript
              console.log("5" + 3);

              Choose an answer to see the explanation.

            6. Question 6 of 7Which statement about HTML is true?

              Choose an answer to see the explanation.

            7. 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" + 3 giving "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.

            CompleteFrontend Clear concepts. Working examples.