Learn/JavaScript/1. Hello, output
Lesson 1 of 15
Sign in

JavaScript, lesson 1

Hello, output

JavaScript started life inside web browsers, but it also runs outside them. In this course your code runs on Node.js 24, a program that executes JavaScript files on a server or your own computer. There is no web page here: your program prints text, and that text is the result.

The tool for printing is console.log(). Pass it a value and it writes that value, followed by a line break, to standard output (stdout), which is where the tests read your answer.

Text inside quotes is a string. JavaScript accepts double quotes ("hi"), single quotes ('hi') and backticks (`hi`). The first two behave the same way; backticks have extra powers you will meet in the next lesson. Each statement usually ends with a semicolon. JavaScript can often insert missing ones for you, but writing them yourself avoids a few surprising bugs.

Printing several values

console.log() accepts any number of values separated by commas. It prints them on one line with a single space between each pair:

2 exercises to try what you just read.

Exercise 1

Your first program

Press Ctrl Enter or Check to test your code.

Exercise 2

A tiny shopping list

Press Ctrl Enter or Check to test your code.

Numbers do not need quotes. If you write console.log(2 + 3), Node.js works out the sum first and prints 5. If you write console.log("2 + 3"), the quotes make it plain text and it prints 2 + 3 exactly.

To join pieces without a space, glue strings together with +:

An empty call, console.log(), prints a blank line.

Comments

Comments are notes for people. Node.js skips them completely.

Use comments to explain why the code does something, not to repeat what it obviously does.

How a program runs

Node.js reads your file from the first line to the last and runs each statement in order. When something goes wrong, such as calling a function that does not exist, Node.js stops and prints an error with a stack trace. The first lines name the error type and message; the lines below point to the file and line number where it happened. Errors go to standard error (stderr), a separate stream from stdout, so they never mix with your real output.

Here first is printed, then the typo stops the program before the third line.

Further reading

  • MDN: JavaScript Guide, Introduction
  • MDN: Grammar and types (comments)
  • Node.js docs: Console

Write a program that prints exactly these two lines:

The program reads no input.

Print a short shopping list. The first line is a heading, then one line per item, then a blank line, then a total. Use one console.log() call with several comma-separated values for each item line (so Node.js adds the spaces), and let JavaScript calculate the total with 2 + 6 + 1 instead of typing 9.

Expected output:

console.log("Hello, World!");
console.log("JavaScript runs one statement at a time.");
console.log("Total:", 42);           // Total: 42
console.log(3, "apples and", 5, "pears");
console.log("2026" + "-" + "11" + "-" + "09");   // 2026-11-09
// A line comment runs to the end of the line.
console.log("This runs"); // it can follow code, too

/* A block comment can span
   several lines. */
console.log("first");
consle.log("second");   // typo: ReferenceError, the program stops here
console.log("never printed");
Hello, World!
I am learning JavaScript.
Shopping list
2 apples
6 eggs
1 bread

Items: 9