JavaScript for impatient programmers (ES2022 edition)
Please support this book: buy it or donate
(Ad, please don’t block.)

8 Consoles: interactive JavaScript command lines



8.1 Trying out JavaScript code

You have many options for quickly running pieces of JavaScript code. The following subsections describe a few of them.

8.1.1 Browser consoles

Web browsers have so-called consoles: interactive command lines to which you can print text via console.log() and where you can run pieces of code. How to open the console differs from browser to browser. Fig. 3 shows the console of Google Chrome.

To find out how to open the console in your web browser, you can do a web search for “console «name-of-your-browser»”. These are pages for a few commonly used web browsers:

Figure 3: The console of the web browser “Google Chrome” is open (in the bottom half of window) while visiting a web page.

8.1.2 The Node.js REPL

REPL stands for read-eval-print loop and basically means command line. To use it, you must first start Node.js from an operating system command line, via the command node. Then an interaction with it looks as depicted in fig. 4: The text after > is input from the user; everything else is output from Node.js.

Figure 4: Starting and using the Node.js REPL (interactive command line).

  Reading: REPL interactions

I occasionally demonstrate JavaScript via REPL interactions. Then I also use greater-than symbols (>) to mark input – for example:

> 3 + 5
8

8.1.3 Other options

Other options include:

  Consoles often run in non-strict mode

In modern JavaScript, most code (e.g., modules) is executed in strict mode. However, consoles often run in non-strict mode. Therefore, you may occasionally get slightly different results when using a console to execute code from this book.

8.2 The console.* API: printing data and more

In browsers, the console is something you can bring up that is normally hidden. For Node.js, the console is the terminal that Node.js is currently running in.

The full console.* API is documented on MDN web docs and on the Node.js website. It is not part of the JavaScript language standard, but much functionality is supported by both browsers and Node.js.

In this chapter, we only look at the following two methods for printing data (“printing” means displaying in the console):

8.2.1 Printing values: console.log() (stdout)

There are two variants of this operation:

console.log(...values: any[]): void
console.log(pattern: string, ...values: any[]): void
8.2.1.1 Printing multiple values

The first variant prints (text representations of) values on the console:

console.log('abc', 123, true);
// Output:
// abc 123 true

At the end, console.log() always prints a newline. Therefore, if you call it with zero arguments, it just prints a newline.

8.2.1.2 Printing a string with substitutions

The second variant performs string substitution:

console.log('Test: %s %j', 123, 'abc');
// Output:
// Test: 123 "abc"

These are some of the directives you can use for substitutions:

8.2.2 Printing error information: console.error() (stderr)

console.error() works the same as console.log(), but what it logs is considered error information. For Node.js, that means that the output goes to stderr instead of stdout on Unix.

8.2.3 Printing nested objects via JSON.stringify()

JSON.stringify() is occasionally useful for printing nested objects:

console.log(JSON.stringify({first: 'Jane', last: 'Doe'}, null, 2));

Output:

{
  "first": "Jane",
  "last": "Doe"
}