How to buildOctober 11, 20269 min read
JavaScript from scratchStep 1 of 9

Your first JavaScript program: how code turns into action

In short

A program is a set of instructions the computer runs in order: it takes data, computes something and outputs a result. You can run JavaScript with nothing installed, in the browser console (Ctrl+Shift+J in Chrome and Edge), or from a file with node file-name.js once Node.js is installed. The first useful program in this lesson is a tip calculator: the bill goes in, the tip and the total come out.

TutorialLevel: BeginnerProgramming BasicsPractice: ~45–90 min

After this you will be able to

  • Run JavaScript in the browser console and from a file with the node command
  • Write an “input → computation → output” program and pass it data through command-line arguments
  • Read an error message: find the file and line number and tell a SyntaxError from a ReferenceError
  • Adapt a program to your own task by adding a new input and calculation

Verified with

Node.js 24 LTS (24.18.0)Microsoft Edge 155

Checked against docs: October 11, 2026

Contents
  1. 01What a program is: instructions, data, result
  2. 02Where JavaScript runs: the browser and Node.js
  3. 03The browser console: your first line of code
  4. 04A file and running it with node
  5. 05Input, computation, output: a tip calculator
  6. 06Syntax errors and how to read them
  7. 07Practice and next steps

What a program is: instructions, data, result

A program is text with instructions for a computer. The computer doesn't guess what you meant and doesn't skip “obvious” steps: it does exactly what is written, in the order it is written. Almost every program follows the same shape: it receives data, does something with it and produces a result. A tip calculator takes the bill and a percentage, multiplies and divides, then shows the tip and the total.

Input dataInstructionsComputationOutput
The same shape works for a tip calculator and for an online store; only the scale changes.

JavaScript is an interpreted language: the browser or Node.js takes the program's source text and runs it without a separate build step. As MDN explains, modern engines speed things up with just-in-time compilation, turning code into a fast binary form while it runs, but you don't see any of that: you write a line, run it and see the result. That makes JavaScript a convenient first language.

Where JavaScript runs: the browser and Node.js

There is one language and two main places to run it, called environments. In the browser, JavaScript controls the page: it reacts to clicks, changes text and submits forms. Node.js is an open-source, cross-platform runtime that runs the same V8 engine as Chrome, but outside the browser: on your computer or on a server. In the lesson on how a website works, these are the two halves of one system, the frontend and the backend.

What to install

Browser
Nothing: Chrome, Edge, Firefox or Safari will do
Node.js
Node.js from nodejs.org

Where to write code

Browser
In the developer tools console or a script tag on a page
Node.js
In a .js file, run with the node command

What's available besides the language

Browser
The page (document), the browser window, events
Node.js
Files, the network, launch arguments (process)

Typical job

Browser
Interactive user interfaces
Node.js
Servers, scripts, developer tools

How to print a result

Browser
console.log, to the browser console
Node.js
console.log, to the terminal

The language itself is standardized in the ECMAScript specification (ECMA-262), and everything in this track that concerns the language (variables, conditions, loops, functions) works the same in both environments. Only the built-in environment objects differ: Node.js has no document, and the browser has no process. If an example uses one of them, it will run only in its own environment.

The browser console: your first line of code

  1. Open any page

    Even an empty tab will do. The console runs code in the context of the open page but doesn't break anything on the site: it all disappears on reload.

  2. Open the console

    In Chrome and Edge, press Ctrl+Shift+J on Windows and Linux or Cmd+Option+J on macOS. F12 opens the developer tools on the last panel you used; switch to the Console tab.

  3. Type a line and press Enter

    The console runs it right away and shows the result below. To add a line break without running, press Shift+Enter.

hello.js
console.log("Hello, InterLAB!"); // → Hello, InterLAB!
console.log(2 + 3);              // → 5
console.log("2" + "3");          // → 23
Type the lines into the browser console one at a time. The comment after // → is what the console will show.

console.log is the “show this value” command. The parentheses hold what to show: text in quotes, a number or an expression. The expression 2 + 3 is evaluated first, so 5 is printed. But "2" + "3" gives 23: in quotes these are strings, not numbers, and plus joins them. The next lesson covers the difference between numbers and strings in detail; for now, just remember that quotes change the meaning.

After each console.log line the console also shows a grey undefined. That's not an error: the console prints the value of the last expression it ran, and console.log itself returns nothing; per MDN its return value is undefined. If you type just 2 + 3 without console.log, the console shows 5, the value of the expression.

A file and running it with node

The console is great for experiments, but a program you'll run again and again lives in a file. For that you need Node.js and a terminal, a window where you type commands as text.

  1. Install Node.js

    Download the LTS version from nodejs.org and install it. Check it in the terminal with node --version: you should see a version such as v24.18.0.

  2. Create a folder and a file

    Make a folder called first-js and, inside it, a file hello.js with the three lines from the previous section. Any code editor will do, VS Code for example.

  3. Run the program

    Open a terminal in the first-js folder and run node hello.js. Node.js reads the file top to bottom and prints the result to the terminal.

  4. Edit and run again

    Changed the file? Run node hello.js again. The node --watch hello.js command restarts the program automatically every time you save the file.

terminal
$ node --version
v24.18.0

$ node hello.js
Hello, InterLAB!
5
23
Running from the first-js folder. The line starting with $ is the command you type; the rest is the response. Your version number may differ.

Node.js skips comments after //; they're for people. The .js extension means a regular JavaScript file. The Node.js docs also use .mjs, which marks module files that use import; we'll get to modules later, first programs don't need them.

Input, computation, output: a tip calculator

Now a program that solves a real task. First, a version for the browser console, with the input data written right in the code.

tip-browser.js
const bill = 12500;    // bill amount, ₸
const percent = 10;    // tip percentage

const tip = bill * percent / 100;
const total = bill + tip;

console.log(`Tip: ${tip} ₸`);            // → Tip: 1250 ₸
console.log(`Total to pay: ${total} ₸`); // → Total to pay: 13750 ₸
Works in the browser console and with node tip-browser.js; the output is the same.
  • const bill = 12500; gives the value a name. From then on the program can say bill instead of the number. The next lesson covers const and let in detail.
  • * and / are multiplication and division. As in math, they run before addition, so bill * percent / 100 is 10% of the bill.
  • Text in backticks is a template literal: the value is substituted in place of ${tip}. On most keyboards the backtick key sits to the left of 1.
  • A semicolon ends an instruction. JavaScript often inserts them for you, but an explicit semicolon avoids rare surprises.

A program with its data inside the code only ever handles one bill. Let's make the amount and the percentage come from outside, from the command line. Node.js provides process.argv for this: the list of arguments the program was launched with. According to the Node.js docs, the first element is the path to node itself, the second is the path to the program file, and then come the values you typed after the file name. Counting starts at zero, so the bill is process.argv[2] and the percentage is process.argv[3].

tip.js
// tip.js — tip calculator
// Usage: node tip.js <bill amount> <percent>
const bill = Number(process.argv[2]);
const percent = Number(process.argv[3]);

const tip = Math.round(bill * percent / 100);
const total = bill + tip;

console.log(`Bill: ${bill} ₸`);
console.log(`Tip ${percent}%: ${tip} ₸`);
console.log(`Total: ${total} ₸`);
The Node.js version: process.argv exists only in Node.js; browsers don't have this object.
terminal
$ node tip.js 12500 10
Bill: 12500 ₸
Tip 10%: 1250 ₸
Total: 13750 ₸

$ node tip.js 8990 15
Bill: 8990 ₸
Tip 15%: 1349 ₸
Total: 10339 ₸
Two runs of the same program with different data.

Command-line arguments always arrive as text, so we wrap them in Number(...) to turn the string “12500” into the number 12500. Math.round rounds to the nearest whole number: 15% of 8,990 is 1,348.5, and the receipt says 1,349 ₸.

Syntax errors and how to read them

Errors are a normal part of the job, and an error message is a hint, not a verdict. Start by telling two kinds apart. A syntax error (SyntaxError) happens when the engine can't parse the program text: per MDN, it ran into tokens or a token order that don't follow the language's grammar. In that case not a single line runs, not even the ones above the error.

broken.js
const bill = 12500;
console.log("Calculating the tip…");
console.log(`Total: ${bill * 1.1} ₸`;
A file with an error: the third line is missing a closing parenthesis.
terminal
$ node broken.js
…\first-js\broken.js:3
console.log(`Total: ${bill * 1.1} ₸`;
                                ^^^^

SyntaxError: missing ) after argument list
    at …

Node.js v24.18.0
The file path is shortened and internal stack lines (at …) are omitted. “Calculating the tip…” never appeared: the program didn't start.

Read the message top to bottom. The first line is the file and line number (broken.js:3). Next comes the line of code itself, and the ^ marks under it show where the engine realized something was wrong. Then the error type and description: missing ) after argument list. Lines starting with at are the call stack inside Node.js; for a syntax error you can skip them. In the browser console the same message reads Uncaught SyntaxError: missing ) after argument list.

The second kind is a runtime error. The syntax is fine and the program started, but at some line it couldn't continue. The most common beginner case is a typo in a name:

terminal
$ node typo.js
Calculating the tip…
…\first-js\typo.js:3
console.log(`Total: ${totl} ₸`);
                      ^

ReferenceError: totl is not defined
    at …

Node.js v24.18.0
typo.js declares the name total, but the third line says totl. The first output line had already run.

SyntaxError: missing ) after argument list

What it means
A parenthesis isn't closed
What to check
The pairs of parentheses on that line

SyntaxError: Unexpected token / Unexpected identifier

What it means
The engine expected a different symbol here
What to check
A missing comma, quote or bracket near the error

ReferenceError: … is not defined

What it means
The name wasn't found
What to check
Typos and letter case: total and Total are different names

NaN in the result

What it means
Not an error but “not a number” in a calculation
What to check
That the input really is numeric

Practice and next steps

In this lesson const showed up as “a name for a value”, and "2" + "3" turned into 23. The next step in the track is variables and data types: how let differs from const, what kinds of values exist, where NaN comes from and how to compare values without surprises. Working in the terminal is covered in a separate lesson on the command line, and reading long error messages with a call stack is covered in the debugging guide.

How this was verified4 · passedchecked 11 October 2026

“Run” means the code was executed in a scratch project or container and the output in the article comes from that run. “Partial” means it was checked against stubs instead of external services. “Not run” means it needs a device, an account or a key; the steps were checked against the official documentation. The verification project files and run logs are kept in the InterLAB archive.

Tell us about your task — we'll review your request and get back to you

Got a project?
We'll scope and estimate it — for free.

Tell us about the product or the problem. After reviewing the task we'll come back with an architecture, a timeline and a price for the first version.

  • Scope and risks estimated before we start
  • NDA — yours or ours
  • Work starts within a week

Please do not send passwords, access keys or source code through this form — we will agree a safe way to share access separately.

By clicking the button you agree to the processing of your personal data.