You have many options for quickly running pieces of JavaScript code. The following subsections describe a few of them.
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. Figure 10.1 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:
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 figure 10.2: The text after >
is input from the user; everything else is output from Node.js.
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
Other options include:
There are many web apps that let you experiment with JavaScript in web browsers – for example, Babel’s REPL.
There are also native apps and IDE plugins for running JavaScript.
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.
console.*
API: printing data and moreIn 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):
console.log()
console.error()
console.log()
(stdout)
There are two variants of this operation:
console.log(...values: Array<any>): void
console.log(pattern: string, ...values: Array<any>): void
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.
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:
%s
converts the corresponding value to a string and inserts it.
console.log('%s %s', 'abc', 123);
Output:
abc 123
%o
inserts a string representation of an object.
console.log('%o', {foo: 123, bar: 'abc'});
Output:
{ foo: 123, bar: 'abc' }
%j
converts a value to a JSON string and inserts it.
console.log('%j', {foo: 123, bar: 'abc'});
Output:
{"foo":123,"bar":"abc"}
%%
inserts a single %
.
console.log('%s%%', 99);
Output:
99%
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.
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"
}