Free preview
What JavaScript is and where it runs
Reading · 12 min
HTML gives a page its structure and CSS makes it look good. JavaScript makes it do things: open a menu, check a form before it's sent, update a basket total, load new posts without reloading the page. It is a real programming language, which means you can give the computer step-by-step instructions, make decisions and repeat work.
Where JavaScript runs
- In the browser. Every browser has a JavaScript engine built in (Chrome's is called V8). This is where we start, because you already have it.
- On servers and computers, through Node.js. The same language powers web servers, command-line tools and even desktop apps such as VS Code itself.
- In mobile apps, through frameworks such as React Native.
Learning JavaScript well opens all of these doors, which is one reason it has been the most used language among developers for many years.
What a program looks like
const name = "Ada";
const hour = new Date().getHours();
if (hour < 12) {
console.log("Good morning, " + name);
} else {
console.log("Good afternoon, " + name);
}Even before the course starts you can probably guess what this does. It stores a name, looks up the current hour and prints a different greeting depending on the time. That is programming: storing data, making decisions and producing a result.
How the computer reads your code
The engine reads your code from top to bottom, one statement at a time. Each statement usually ends with a semicolon. Curly braces { } group statements into blocks, such as "everything that happens if it's morning". JavaScript is case-sensitive: name, Name and NAME are three different things.
How to learn programming
- 1
Type the examples
Typing code, rather than copying it, builds the memory in your fingers and makes you notice details such as brackets and quotes.
- 2
Change things
After each example works, change something and predict what will happen before you run it. This is the fastest way to understand.
- 3
Expect errors
Every programmer sees error messages all day. They are not failures; they are the computer telling you exactly where to look. You'll learn to read them in this course.
- 4
Little and often
Twenty minutes a day beats four hours once a week. Programming is a skill like a language or an instrument.
Try it
Your first line of JavaScript
Open any web page, press F12 (or Cmd+Option+J on a Mac) to open the developer tools and click the Console tab. Type console.log("Hello from " + "JavaScript") and press Enter. Then type 2 + 2 * 10 and press Enter.
Show a hint
If you see a warning about pasting code, it is there to protect you from scams. Type the code yourself.
Show a worked answer
The console prints Hello from JavaScript and then 22. Multiplication happens before addition, just like in maths, so 2 * 10 is worked out first.
Like this lesson?
JavaScript Programming has 22 lessons with exercises, quizzes and a final project.