Beginner · First steps: values, variables and operators · topic 1 of 61 · 20 min
How JavaScript runs
Explanation
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.
Three ways to run JavaScript
- Browser console: open any page, press F12 (or Ctrl+Shift+J), click Console and type 2 + 3, then Enter.
- 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.
- A file: save code in hello.js and run node hello.js in the same folder. This is how real programs are run.
// hello.js
console.log('Namaste from JavaScript!');
console.log('2 + 3 =', 2 + 3);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.
| Place | Engine or runtime | Extra powers |
|---|---|---|
| Chrome, Edge | V8 | Web page (DOM), fetch, storage |
| Firefox | SpiderMonkey | Web page (DOM), fetch, storage |
| Your computer or a server | Node.js (V8) | Files, network servers, processes |
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
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.
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);Practice (0/3 done)
Make a variable named greeting that holds exactly the text "Namaste, JavaScript!" and print it with console.log.
Write a function secondsInDays(days) that returns how many seconds there are in that many days (24 hours, 60 minutes, 60 seconds).
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
Read more (free)
Related help
Related topics
- Values and typesFirst steps: values, variables and operators · same module
- Variables: let, const and varFirst steps: values, variables and operators · same module
- Operators and type coercionFirst steps: values, variables and operators · same module
- Node.js: JavaScript on the serverBackend Development · Node.js and Express
- Node.js basicsPerformance and Node.js
- Your first program: output, input and the Main methodC# and .NET · Getting started with C# and .NET
Questions people ask about this
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.
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.
Where does every C program start executing?Related
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
- 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.
- Module 1 exam: First steps · 12 questions · 20 min, without help. 5 topics of this module still to finish first.
- Then the next module, Strings, decisions and loops. 61 topics left in the course before the final exam.