Learn JavaScript

Lesson 5 of 6 · What JavaScript Is, Where It Runs, and How an Engine Runs It

Module 0 · What JavaScript Is, Where It Runs, and How an Engine Runs It

Your First Run: The Console, Node, and the Playground

FreeReading

In this lesson

  • Run one line of JavaScript in a browser console, in Node.js from a file, and on the Playground.
  • Explain what console.log prints, and name the other ways a program can show output.
  • Write a comment, and end every statement with a semicolon.

Zara does not trust anything she has not tested herself. So before she writes a real program, she runs one line, console.log("Hello");, in three different places. A browser, a file on her laptop, and the Playground.

All three print Hello. That is the point of this lesson. JavaScript is one language, and after today you will know three places to run it, so you never have to wonder where code goes.

Pick whichever place is easiest for you now. The Playground needs nothing installed, and every lesson's Run button uses it.

Place one: the browser console

Every desktop browser hides a console inside its developer tools: a text box where you type JavaScript and see the result at once. It is the fastest place to try one line.

BrowserWindows and LinuxMac
ChromeCtrl+Shift+JCmd+Option+J
FirefoxCtrl+Shift+KCmd+Option+K
EdgeCtrl+Shift+JCmd+Option+J

Any of them also opens with F12; then click the tab called Console. Click in the box at the bottom, type console.log("Hello"); and press Enter. You will see two lines come back:

Hello
undefined

The first line is your output. The grey undefined is the console telling you that the line itself gave back no value. It is not an error; every console does it, and you can ignore it.

One more surprise. The first time you paste code into Chrome's or Firefox's console, it may refuse and ask you to type allow pasting. This protects people from scammers who say "paste this into your console". Type it once, and pasting works.

So the browser console runs one line the moment you press Enter.

Place two: Node.js, from a file

The console is perfect for one line and awkward for twenty. For a real program you save it in a file and run the file with Node.

  1. Install Node.js from nodejs.org. Pick the version marked LTS (long-term support). Any version from 22 up runs everything in this track.
  2. Open a terminal and type node --version. It should print a version starting with v22 or higher.
  3. In any text editor, save the line console.log("Hello"); in a file called hello.js.
  4. In the terminal, go to the folder with that file and type node hello.js.

The terminal prints Hello, with no undefined after it. Only the console adds that extra line; a program run from a file prints exactly what it logs.

Typing node alone opens Node's own console, called the REPL (read, evaluate, print, loop). It works like the browser console. Leave it with .exit.

So Node runs a whole file with one command, node and the file's name.

Place three: the Playground

The Playground runs the same Node.js 22 on a Progsity machine, so you install nothing. Every Run link in these lessons opens it with the example already typed in. To start from a blank page, choose the language JavaScript (Node.js), version Node.js 22.

Four parts of the screen matter:

  • The editor, where your code goes.
  • Run, which sends the code to the machine and runs it.
  • Standard Input (stdin), a box for text your program can read. You type it before you press Run.
  • The output, with a badge saying how the run ended: Success, Compile error or Runtime error, as lesson 04 explained.

The input box matters from Module 1 on. Every judged problem gives your program its input through stdin, and Example 4 below takes a first look.

Input, program, output on the Playground Input (stdin) Zara Your program main.js, on Node.js 22 Output (stdout) You typed: Zara console.log writes to the output. A browser shows it in the console; Node and the Playground show it as stdout.
Figure 1. The shape of every program in this track: something goes in, the program works on it, something comes out.

So the Playground is Node.js 22 with an input box and an output pane, one click away.

What console.log prints

console.log prints whatever you give it, then moves to a new line. You give it things between its round brackets, separated by commas. These things are called arguments.

A console.log statement

console.log("Total:", 250 + 120);
  • console is the output tool every place in this lesson provides.
  • .log is what you ask it to do: print a line.
  • ( … ) holds the arguments, the things to print, separated by commas.
  • "Total:" is text, written inside quotes; 250 + 120 is a sum, worked out first.
  • ; ends the statement, as every statement in this track does.

Three rules cover nearly everything. Text in quotes is printed without its quotes. A number or a sum is printed as its value. Several arguments are printed on one line with one space between each.

Example 1: the line Zara ran

The smallest program in this track, and the one that runs the same in all three places.

console.log("Hello");
Hello

On the Playground and in Node this is the whole output. In a browser console you also see the grey undefined under it.

Run in Compiler
Example 2: two arguments, and a number

Text and a number on one line, then text and a sum.

console.log("Zara", 3);
console.log("Total:", 250 + 120);
console.log();
console.log("a", "b", "c");
Zara 3
Total: 370

a b c

The comma adds a space for you. 250 + 120 is worked out before printing. An empty console.log() prints an empty line, which is handy for spacing out a report.

Run in Compiler

Other ways to show output

console.log is not the only way a program can show something, but it is the only one that works in all three places.

WayWhere it worksIn this track
console.logBrowser, Node, the PlaygroundEverywhere
alertBrowser only: a pop-up boxNamed, not used (lesson 02's teaser)
document.writeBrowser only: writes into the pageNamed, not used; old and discouraged
process.stdout.writeNode and the Playground: prints with no new lineLater, when output speed matters

So console.log is the one output tool this track relies on, because it works everywhere.

Semicolons and comments

A statement is one complete instruction, such as one console.log(...) call. This track ends every statement with a semicolon, ;. JavaScript can often add a missing one by itself, but not always, and Module 1 shows the one case where that bites. Writing them yourself removes the question, so this is said once and then simply done.

A comment is text the engine skips completely. It is a note for the human who reads the code next, often you a month later. There are two forms: // comments out the rest of a line, and /* ... */ comments out everything between the two marks, across several lines if needed.

Example 3: comments the engine skips

Five lines of text, two lines of output.

// Zara's first program: two lines of output.
console.log("Hello"); // a comment can follow the code
/* A comment can also
   cover several lines. */
console.log("Goodbye");
Hello
Goodbye

Everything after // on a line, and everything between /* and */, is gone before the program runs. Module 1 covers what a good comment says.

Run in Compiler
Example 4: a first look at the input box

Type Zara into the Playground's Standard Input box, then run this. The first line reads everything in that box.

const input = require("fs").readFileSync(0, "utf8");
console.log("You typed: " + input.trim());
You typed: Zara

Do not worry about the first line yet. Module 1 explains it word by word, because every judged problem starts with lines like it. For now, notice that the program printed what you typed.

Run in Compiler

Where this is used

  • Browser developer tools. Frontend developers keep the console open while they work, to try a line and to read what their page logs.
  • Node's REPL. Typing node in a terminal is the quickest way to check what a line of JavaScript does, without making a file.
  • Server logs. On hosting platforms such as Vercel and Heroku, whatever a Node server prints with console.log is collected as its log. So the tool you learn today is also how a live server reports problems.
  • The Progsity Playground and judge. Every judged problem in this track compares what your program prints, line by line, with the expected answer.

Common mistakes

1. A capital C.

Console.log("Hi");

Node.js 22 says ReferenceError: Console is not defined. JavaScript cares about capitals, and the output tool is console, all lower case. Phones and some editors capitalise the first letter of a line for you, which is how this usually happens.

2. Forgetting the quotes.

console.log(Hello);

Node.js 22 says ReferenceError: Hello is not defined. Without quotes, Hello is read as the name of something, and nothing has that name. Text you want printed as it is always goes inside quotes.

3. Curly quotes pasted from a document.

console.log(“Hi”);

The Playground shows a Compile error: SyntaxError: Invalid or unexpected token. Word processors and chat apps turn " into curly quotes, and JavaScript does not accept them. Type code in a code editor or the Playground, never in a word processor.

4. Typing the terminal command inside Node's console.

If you type node first and then node hello.js, Node's REPL answers Uncaught SyntaxError: Unexpected identifier 'hello'. You are already inside Node, which expects JavaScript, not a command. Type .exit, then run node hello.js in the plain terminal.

Brain teaser

Before you run it, predict both lines this program prints:

console.log(1, 2);
console.log("1" + 2);

The first line uses a comma, which this lesson explained. The second uses + between text in quotes and a number. Run it to check, and keep the second answer in mind: Module 2 explains exactly why it happens.

Exercise 1Easy

Write a program that prints exactly these three lines, using three console.log statements:

Name: Zara
Track: JavaScript
Lesson: 05

Check yourself. Run it in at least two of the three places. The output must match letter for letter, with each line on its own. If a line is missing its semicolon, add it.

Exercise 2Medium

Print a word and a number on one line, using one console.log with two arguments. The output must be:

Apples: 12

Rules. The 12 must be a number, so it is not inside quotes. Do not type the space yourself.

Check yourself. There is exactly one space between the colon and 12. If you see two, you typed one and the comma added another.

Exercise 3Hard

Open Example 4 on the Playground and leave the Standard Input box empty. Before you press Run, write down what you expect it to print. Then run it.

Rules. Explain the output in two sentences: what the program read, and why the line is not an error.

Check yourself. The program still prints a line. Think about what the input box held, and what joining text to "nothing" gives. Module 1 returns to empty input, because judged tests use it.

Common doubts

  • Why does the browser console print undefined after my line?

    The console shows the value of every line you type. console.log prints your text but gives back no value, and "no value" is shown as undefined. A program run from a file never shows it.

  • Do I really need the semicolons?

    Often JavaScript adds them for you, which is why some code online leaves them out. This track writes them every time, because the one case where JavaScript guesses wrong is hard to spot.

  • Single quotes or double quotes?

    JavaScript accepts both. This track writes double quotes, to keep one style. Module 1 says more.

  • Which Node.js version should I install?

    The one marked LTS on nodejs.org. Anything from version 22 up runs every lesson here, and the Playground runs 22.

  • Can I do this on my phone?

    Phone browsers do not open a console easily, and installing Node on a phone is awkward. The Playground works in a phone browser, so use it there.

Key takeaways

  • One line of JavaScript runs in three places: a browser console, Node.js from a file, and the Playground.
  • A browser console adds a grey undefined; a program run from a file prints only what it logs.
  • console.log prints its arguments with one space between them, then starts a new line.
  • alert and document.write work only in a browser; console.log works everywhere.
  • This track ends every statement with ;, and comments are written with // or /* ... */.

Module 1 starts writing real programs: numbers, text, names for values, and reading input the way every judged problem does.

Module test

Ten questions on this module. Pass at 70%, and you can take it as many times as you like.

Take the module test

End of lesson 5

Mark it done, and your progress moves with you.

Next: Module Test: What JavaScript Is