Skip to content

Beginner · First steps: values, variables and operators · topic 1 of 61 · 20 min

How JavaScript runs

Explanation

JavaScript is a programming language that runs inside every web browser and, with Node.js, on any computer or server. A program called an engine reads your code and runs it line by line. You can try code in the browser console, in the Node.js REPL, or by saving it in a .js file and running node file.js.

What a programming language is

A computer only follows very small, exact instructions. A programming language is a set of words and rules that lets you write those instructions in a form people can read. JavaScript is one such language. You write text, and another program turns that text into work the computer can do.

JavaScript was created in 1995 to make web pages interactive. Today it also runs on servers, in mobile apps, in desktop apps like VS Code, and in small command-line tools. That is why it is one of the most used languages in the world.

The engine: the program that runs your code

An engine is the program that reads JavaScript and runs it. Chrome and Edge use V8, Firefox uses SpiderMonkey and Safari uses JavaScriptCore. Node.js is a program built around V8 that runs JavaScript outside the browser, with extra powers such as reading files and starting a web server.

Think of it like this: Think of JavaScript as a recipe written in English, and the engine as the cook. The same recipe can be cooked in a home kitchen (the browser) or in a big hotel kitchen (a server running Node.js). The words are the same; only the kitchen and its tools change.

Three ways to run JavaScript

  1. Browser console: open any page, press F12 (or Ctrl+Shift+J), click Console and type 2 + 3, then Enter.
  2. Node.js REPL: install the LTS version from nodejs.org, open a terminal and type node. REPL means Read-Eval-Print Loop: it reads one line, runs it and prints the result. Type .exit to leave.
  3. A file: save code in hello.js and run node hello.js in the same folder. This is how real programs are run.
javascript
// hello.js
console.log('Namaste from JavaScript!');
console.log('2 + 3 =', 2 + 3);
Run it with: node hello.js. Prints Namaste from JavaScript! and then 2 + 3 = 5.

Statements, semicolons and comments

A statement is one instruction, like console.log(...). Statements run from top to bottom. Each one usually ends with a semicolon. JavaScript can insert missing semicolons for you (this is called automatic semicolon insertion), but beginners should write them to avoid rare surprises.

A comment is a note for people; the engine ignores it. // starts a comment that runs to the end of the line, and /* ... */ can cover many lines.

ECMAScript: the official name and versions

The official standard for the language is called ECMAScript. A committee called TC39 adds a new version every year: ES2015 (often called ES6) brought let, const, arrow functions, classes and modules; later versions added async/await, optional chaining and much more. When you read "ES2023 feature", it means a feature added in that year's version.

PlaceEngine or runtimeExtra powers
Chrome, EdgeV8Web page (DOM), fetch, storage
FirefoxSpiderMonkeyWeb page (DOM), fetch, storage
Your computer or a serverNode.js (V8)Files, network servers, processes
Careful: A common beginner mistake is typing node hello.js inside the Node REPL (after you already typed node). That fails with a SyntaxError. Leave the REPL first with .exit or Ctrl+C twice, then run node hello.js in the normal terminal.
Tip: This course runs your tasks right here in the browser, so you can start without installing anything. Install Node.js when you are ready to run programs on your own computer.

Need help?

Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.

Try:

Watch the lesson

How JavaScript runs · 1/6

How JavaScript runs

Welcome to JavaScript Programming. In this first lesson you will see where JavaScript runs and run your first line of code.

Prefer a video by a teacher? Find free videos on “how javascript works engine node js for beginners” on YouTube ↗

In real life: A daily sales report script

A small online shop in Jaipur exports its orders as a file every evening. The owner's nephew writes a 20-line JavaScript file and runs node report.js to print the day's total and the best-selling item. No browser and no web page are needed: Node.js runs the script directly on the laptop.

javascript
const orders = [450, 1200, 299, 850];
let total = 0;
for (const amount of orders) total += amount;
console.log('Orders today:', orders.length);
console.log('Total sales: Rs', total);
Prints Orders today: 4 and Total sales: Rs 2799.

Practice (0/3 done)

Task 1 · Code · Javascript

Make a variable named greeting that holds exactly the text "Namaste, JavaScript!" and print it with console.log.

Task 2 · Code · Javascript

Write a function secondsInDays(days) that returns how many seconds there are in that many days (24 hours, 60 minutes, 60 seconds).

Task 3 · Practice

Install the LTS version of Node.js from nodejs.org. Check it with node --version. Create hello.js that prints your name and the result of 12 * 12, and run it with node hello.js.

Quick quiz

Question 1

1.What is a JavaScript engine?

Question 2

2.Which command runs the file app.js with Node.js?

Question 3

3.ES2015 is also commonly called…

Question 4

4.What does the engine do with // comments?

Read more (free)

Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.

What is a JavaScript engine?

Answer: A program that reads and runs JavaScript code

An engine such as V8 or SpiderMonkey reads your code and runs it.

Which command runs the file app.js with Node.js?

Answer: node app.js

node followed by the file name runs that file.

ES2015 is also commonly called…

Answer: ES6

ES2015 was the sixth edition of ECMAScript, so people call it ES6.

What does the engine do with // comments?

Answer: Ignores them

Comments are notes for people; the engine skips them.

Which Node.js version should a beginner install?Related

Answer: The LTS version

LTS means Long Term Support. It is the stable version that keeps getting fixes, so it is the safe choice.

From Node.js: JavaScript on the server

node app.js hello: what is process.argv[2]?Related

Answer: "hello"

argv[0] is node, argv[1] is the script, argv[2] is the first argument.

From Node.js basics

How many files in one project may contain top-level statements?Related

Answer: Exactly one

A program has one entry point, so only one file can hold top-level statements.

From Your first program: output, input and the Main method

Where does every C program start executing?Related

Answer: The main function

main is the entry point.

From The structure of a C program

Pass the quiz (60% or more) or mark this topic as done when you are ready.

Your progress is saved on this device only.

Next steps

  1. 4 more topics in “First steps: values, variables and operators” before its exam: Values and types, Variables: let, const and var, Operators and type coercion, Numbers and Math.
  2. Module 1 exam: First steps · 12 questions · 20 min, without help. 5 topics of this module still to finish first.
  3. Then the next module, Strings, decisions and loops. 61 topics left in the course before the final exam.