Printing is functionality that is not part of the JavaScript language standard. However, all operations that we examine here, are supported by both browsers and Node.js.
Printing means “displaying something on the console”, where “console” is either the browser console or the terminal in which you run Node.js.
The full API is documented and on the Node.js website. We’ll just take a quick look at the following two operations:
console.log()
console.error()
There are two variants of this operation:
7.1.1. Printing multiple values
The first variant prints (text representations of) values on the console:
At the end, console.log()
always prints a newline. Therefore, if you call it with zero arguments, it just prints a newline.
7.1.2. Printing a string with substitutions
These are some of the directives you can use for substitutions:
%s
converts the corresponding value to a string and inserts it.
%o
inserts a string representation of an object.
%j
converts a value to a JSON string and inserts it.
%%
inserts a single%
.
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.
Output:
{
"first": "Jane",
}