Skip to content
aviral gupta

// B1.1 · ~30 min · Beginner

Running JavaScript in the console and in Node.js

After this lesson you can run a JavaScript file with Node.js or in the browser, predict what console.log prints, and use an error message to find the line that stopped your program.

Lesson 1 of 5 in B1 Getting started

Start of the module

You will be able to

  • Run JavaScript in the browser console, in the editor on this page and with node main.js
  • Predict what console.log prints for text, numbers, sums and lists
  • Read an error message: its type, its text and its line, and know why nothing after it ran
  1. Warm-up · Activity 1 of 7

    Warm-up: where can JavaScript code run? Pick all that apply.

    Select all that apply.

  2. Predict · Activity 2 of 7

    Predict before you read on: what does this print?

    console.log("Total", 3.5 + 4.25, "EUR");
  3. Practice · Activity 3 of 7

    Fill in the method name so that the program prints Hello, Node.js.

    console.____("Hello, Node.js");
    console.("Hello, Node.js");
  4. Practice · Activity 4 of 7

    The first line has a typo. Which kind of error does running it give?

    consol.log("Hello");
  5. Practice · Activity 5 of 7

    Match each line of code to what it prints.

  6. Brain teaser · Activity 6 of 7

    Brain teaser. This program stops with an error. What does it print before that?

    console.log("one");
    console.log(two);
    console.log("three");
  7. Apply · Activity 7 of 7

    Mini-task. Write a program with three console.log lines that prints a short profile: a name, a year of birth, and an age that JavaScript works out with -. Run it in the editor, then save it as main.js and run node main.js if you have Node.js installed.

    Check your work against this list

Build it yourself

Read the worked example, then write the exercises. Your code runs in your browser or on your computer and is never uploaded.

Worked example

A café bill

This program prints a small bill for one table. It uses only console.log and console.error: numbers, sums, a list of items and text. Run it, then change the price of the cake or add an item and run it again. The printer warning goes to stderr, so it is not part of the bill below.

main.js

// A café bill, printed line by line.
console.log("Order for table", 4);
console.log("coffee", 2 * 3.2);
console.log("cake", 4.5);
console.log("items:", ["coffee", "coffee", "cake"]);
console.log("total:", 2 * 3.2 + 4.5, "EUR");
console.error("(printer: paper low)");
console.log("Thank you!");

Run it with

node main.js

Output

Order for table 4
coffee 6.4
cake 4.5
items: [ 'coffee', 'coffee', 'cake' ]
total: 10.9 EUR
Thank you!
  • Each console.log call prints one line, in the order of the code.
  • 2 * 3.2 + 4.5 is calculated first: multiplication before addition, as in maths.
  • A list of text is printed with single quotes around each item, so you can see where each one starts and ends.
  • console.error writes to stderr: the terminal shows it, but it is not part of the program’s normal output.
Change it and run it

Tab indents and Shift+Tab outdents. To leave the editor with the keyboard, press Esc, then Tab.

The first run downloads the JavaScript runner (up to 0.1 MB) and keeps it cached. Your code runs in your browser’s own engine and stays on your device.

Exercises

Exercise 1 of 2

Your first receipt

Print a receipt of three lines: Coffee 3.5, then Cake 4.25, then Total 7.75. Pass each label and price as two values to console.log, and let JavaScript add up the total with + rather than typing 7.75.

Tab indents and Shift+Tab outdents. To leave the editor with the keyboard, press Esc, then Tab.

The first run downloads the JavaScript runner (up to 0.1 MB) and keeps it cached. Your code runs in your browser’s own engine and stays on your device.

Hints
  1. Hint 1

    console.log("Coffee", 3.5) prints Coffee 3.5: two values, separated by a space.

  2. Hint 2

    For the total, put the sum where the value goes: console.log("Total", 3.5 + 4.25).

  3. Hint 3

    Three lines of output need three console.log calls.

Show a solution

One way to solve it. Yours can look different and still pass the checks.

console.log("Coffee", 3.5);
console.log("Cake", 4.25);
console.log("Total", 3.5 + 4.25);
Run it on your computer

Install ECMAScript 2026 or newer. Save these files in one folder, open a terminal in that folder, and run the commands below.

main.js

// Print three lines:
// Coffee 3.5
// Cake 4.25
// Total 7.75 (worked out with +)

main.test.js

import {test} from 'node:test';
import assert from 'node:assert/strict';
import {runMain} from './learnrun.js';

const lines = async () => (await runMain()).trimEnd().split('\n');

test('prints three lines', async () => {
  const got = await lines();
  assert.equal(got.length, 3, `the program printed ${got.length} line(s), expected 3`);
});

test('prints the coffee and the cake', async () => {
  const got = await lines();
  assert.deepEqual(got.slice(0, 2), ['Coffee 3.5', 'Cake 4.25'], `the first two lines are ${JSON.stringify(got.slice(0, 2))}`);
});

test('prints the total', async () => {
  const got = await lines();
  assert.equal(got[2], 'Total 7.75', `the third line is ${JSON.stringify(got[2])}, expected "Total 7.75"`);
});

package.json

{
  "type": "module"
}

package.json tells Node.js that the .js files are modules; keep it in the folder.

Run the program:

node main.js

Run the checks (needs learnrun.js in the same folder):

node --test
Download learnrun.js

Exercise 2 of 2

Fix the broken list

This program should print a shopping list, but it stops with an error. Run it, read the error, fix that line and run it again: there is more than one mistake. When it works it prints four lines, ending with fruit in total: 10.

Tab indents and Shift+Tab outdents. To leave the editor with the keyboard, press Esc, then Tab.

The first run downloads the JavaScript runner (up to 0.1 MB) and keeps it cached. Your code runs in your browser’s own engine and stays on your device.

Hints
  1. Hint 1

    Start with the first error message: its first line names the kind of error, and the file and line number show where.

  2. Hint 2

    JavaScript is case-sensitive: console and Console are two different names.

  3. Hint 3

    Values given to console.log are separated by commas.

Show a solution

One way to solve it. Yours can look different and still pass the checks.

console.log("Shopping list");
console.log("apples", 6);
console.log("pears", 4);
console.log("fruit in total:", 6 + 4);
Run it on your computer

Install ECMAScript 2026 or newer. Save these files in one folder, open a terminal in that folder, and run the commands below.

main.js

console.log("Shopping list");
console.log("apples", 6);
Console.log("pears", 4);
console.log("fruit in total:" 6 + 4);

main.test.js

import {test} from 'node:test';
import assert from 'node:assert/strict';
import {runMain} from './learnrun.js';

test('runs without an error', async () => {
  await runMain();
});

test('prints the whole list', async () => {
  const got = (await runMain()).trimEnd().split('\n');
  assert.deepEqual(got, ['Shopping list', 'apples 6', 'pears 4', 'fruit in total: 10'], `the program printed ${JSON.stringify(got)}`);
});

package.json

{
  "type": "module"
}

package.json tells Node.js that the .js files are modules; keep it in the folder.

Run the program:

node main.js

Run the checks (needs learnrun.js in the same folder):

node --test
Download learnrun.js

Common mistakes

A typo in a name

console.log("one");
consol.log("two");
console.log("three");

What Node.js prints

ReferenceError: consol is not defined

Why, and the fix

JavaScript does not guess what you meant: consol is an unknown name, so line 2 stops the program. one is printed first, three never. Read the name in the message, find it at the line number, and correct the spelling.

A capital letter where there is none

Console.log("hi");

What Node.js prints

ReferenceError: Console is not defined

Why, and the fix

Names in JavaScript are case-sensitive: console, Console and CONSOLE are three different names, and only console exists. Write it in lower case.

A missing comma between values

console.log("total:" 5);

What Node.js prints

SyntaxError: missing ) after argument list

Why, and the fix

Values passed to console.log need commas between them. Without one, JavaScript cannot read the line, and the message names the ) it expected. Because the file cannot be read, not even the lines above this one run. Write console.log("total:", 5).

JavaScript in the browser: your browser’s own engine, in a sandboxed worker. Syntax errors are located with acorn 8.18.0, MIT. Licence and source

Exit ticket

5 questions, no hints. Score 80% or more to complete the lesson.

Finish every activity above to unlock the exit ticket.

Report a problem

Spotted something wrong or unclear? Say what, and it will be checked and fixed.

#

At least 20 characters.

Only if you want a reply.

Key ideas

One language, two places to run it

JavaScript runs in every browser and, with Node.js, on your own computer. In a browser, open the developer tools and their Console tab: type 2 + 3, press Enter, and the answer appears. For a whole program, save it as main.js and run node main.js in a terminal. The editor on this page runs your file the way node main.js does. The language is the same everywhere; what differs is what is around it. A page has a document to change, Node.js has files and a terminal, and both have console.

console.log prints values, in order

console.log("Total", 3.5 + 4.25) works out each value and prints them on one line, separated by spaces: Total 7.75. Text is printed without its quotes, numbers as numbers, and a list with brackets and spaces, as [ 1, 2, 3 ]. Each call starts a new line. console.error prints the same way, but to a second channel, stderr, which the terminal shows too and which tools keep apart from normal output.

Top to bottom, until an error stops it

A program runs from the first line to the last. If a line fails, JavaScript stops there and prints an error with its type, its text and where it happened: ReferenceError: consol is not defined, with the file and line, such as main.js:2, above it. Lines before it have already run; lines after it never run. A SyntaxError is different: the file cannot even be read, so not a single line runs. Read the type, then the text, then the line number, and look there first.

Sources

Last reviewed September 30, 2026