What JavaScript Is and Your First Lines
See where JavaScript runs, how statements, comments and console.log work, and write your first lines of code that the computer actually executes.
Welcome back!
Step 1 of 12Learn
The language you already use every day
Open almost any website and JavaScript is running. When you click "like" and the heart turns red without the page reloading, when a search box suggests words as you type, when a map lets you drag it around, that is JavaScript at work. It is the only programming language that every web browser understands natively, which has made it one of the most widely used languages in the world.
It started small. In 1995, Brendan Eich created the first version at Netscape in about ten days, to add simple interactivity to web pages. The language is standardized under the name ECMAScript, and modern versions add features every year.
Today JavaScript runs far beyond the browser:
- Web pages: buttons, forms, animations, live updates.
- Servers: with Node.js, the same language powers back ends and APIs.
- Apps and tools: desktop apps, mobile apps, and the build tools developers use every day.
Despite the name, JavaScript has nothing to do with Java. The name was a marketing choice in the 1990s, when Java was fashionable.
In this course you will learn the core language: values, variables, decisions, functions, loops, arrays and objects, then see how it controls a web page, handles errors and waits for data. You will write real code in the browser as you go.
Step 2 of 12Timeline
How JavaScript grew up
- 1995Created at Netscape
Brendan Eich writes the first version in about ten days to make web pages interactive.
- 1997First ECMAScript standard
The language is standardized as ECMA-262, so every browser can implement the same rules.
- 2009Node.js appears
JavaScript escapes the browser and starts running servers and command-line tools.
- 2015ES2015 (ES6)
A huge update adds
let,const, arrow functions, classes and modules. - 2016 onwardA new version every year
Smaller yearly releases add features such as
async/await,??and?..
Step 3 of 12Learn
Statements, comments and console.log
A JavaScript program is a list of statements, instructions the computer runs from top to bottom. Here is a complete program:
// Greet the user
console.log("Hello, world!");
console.log(2 + 3);console.log(...)prints a value to the console, a text panel for developers. In a browser, press F12 (or right-click, then Inspect) and open the Console tab to see it. You can also type JavaScript directly into the console and press Enter.- Text in quotes, such as
"Hello, world!", is a string. The quotes tell JavaScript it is text, not code. 2 + 3is an expression: JavaScript calculates it and logs 5.- Lines starting with
//are comments. JavaScript ignores them; they are notes for humans. Multi-line comments go between/*and*/. - Statements usually end with a semicolon. JavaScript can often insert missing ones automatically, but writing them yourself avoids rare surprises.
JavaScript is case-sensitive: console.log works, Console.Log does not. Most beginner errors are small typos like that, a missing quote, or an unclosed parenthesis. Error messages in the console tell you the line number, so read them before guessing.
Step 4 of 12Practice · Choose one
What does this program print?
``js
console.log("2 + 3");
console.log(2 + 3);
``
Step 5 of 12Practice · Match the pairs
Match each piece of code to what it is.
Tap an answer, then tap the place where it belongs (or drag it there).
- // total price
- "Hello"
- console.log(x)
- 7 * 6
Step 6 of 12Learn
Where to write code
You need no installation to start. Three easy options:
- The browser console (F12, then Console). Ideal for trying one-liners. Type
10 * 12and press Enter; the answer appears at once. - The code challenges in this course. Each one gives you a starter, runs your code against tests and tells you which tests passed.
- An HTML file. Create a file called
index.htmlwith this content and open it in a browser:
<!DOCTYPE html>
<html>
<body>
<h1>My page</h1>
<script>
console.log("The script ran!");
</script>
</body>
</html>JavaScript in a web page lives inside a <script> tag, or in a separate .js file loaded with <script src="app.js"></script>. Separate files are the norm for anything bigger than a few lines.
When you are ready for larger projects, a free code editor such as Visual Studio Code adds color highlighting, autocomplete and error hints. To run JavaScript outside the browser, install Node.js and type node app.js in a terminal.
Step 7 of 12Note
Step 8 of 12Practice · Diagram
The edit, run, read, fix loop
At which point in the loop do you find out which line is broken?
Tap the shape that answers the question, then press Check.
- Write a few lines
Small steps make errors easy to locate.
- Run the code
Reload the page, press Enter in the console, or click Run in a challenge.
- Read the console
The red message names the problem and the line number after the colon.
- Fix the first error
Later errors are often caused by the first one, so start there.
Step 9 of 12Practice · Spot the mistake
This short program has one mistake that stops it from running. Find it.
Step 10 of 12Practice · True or false
JavaScript is a simplified version of the Java programming language.
Step 11 of 12Tap to reveal
How a browser runs your script
1. The page loads
The browser downloads the HTML and starts reading it from top to bottom, building the page structure as it goes. When it meets a script tag, it fetches the JavaScript file if the code lives in a separate file.
2. The engine parses the code
Every browser contains a JavaScript engine: V8 in Chrome and Edge, SpiderMonkey in Firefox, JavaScriptCore in Safari. The engine first checks that the code is valid. A single missing bracket stops the whole file at this stage with a SyntaxError, before any line runs.
3. The code runs, top to bottom
Valid code is compiled to fast machine instructions and executed one statement at a time. Values are created, functions are defined, and anything you log appears in the console.
4. The page waits for events
After the script finishes, the page stays alive. Functions you attached to buttons, timers and network requests run later, whenever their event happens. This is why a page can keep reacting to you long after the script itself has finished.
Why this matters to you
If a script seems to do nothing, check the console for a red error first. If an element cannot be found, the script may have run before the browser reached that part of the HTML. Both are among the most common beginner problems, and both make sense once you know this order.
Step 12 of 12Practice · Select all that apply
Where can JavaScript code run? Select all that apply.
Select all that apply.
Lesson complete!