Output, comments and the semicolon rule
A program writes to two streams and reads from a third. Standard output carries the answer, standard error carries the complaints, and the terminal shows both by default, which is why a diagnostic can look like a result. console.log writes to the first, console.error to the second, and process.stdout.write writes without adding a newline.
The rest of the lesson is about the parts of a file that do not run. A comment is removed before the parser sees the code, and a newline sometimes ends a statement and sometimes does not: the rule that decides is automatic semicolon insertion, and one line break in the wrong place changes what the program computes.
Standard output and standard error
The program below writes to both streams. Two writes go through console.log, one through console.error, and two through process.stdout.write, which takes a string and adds nothing to it. The last of those writes the newline itself.
console.log('this line goes to standard output');
console.error('this line goes to standard error');
process.stdout.write('no newline here');
process.stdout.write(', and the rest of the line\n');
console.log('after the write');
Redirect standard error to the null device and only the results remain. This is the form to use when a program prints a lot of diagnostics and you want to read the answer:
node streams.js 2>/dev/null
this line goes to standard output
no newline here, and the rest of the line
after the write
Merge the two streams instead, and the diagnostic appears between the two result lines, in the order the calls were made:
node streams.js 2>&1
this line goes to standard output
this line goes to standard error
no newline here, and the rest of the line
after the write
Nothing was added to the string by write: the words of the two calls share one line, and the newline in the second call ends it. A program that mixes write and log without thinking about the missing newline is a common source of lines that run together.
What console.log prints
Each argument is converted to text and the arguments are separated by a space. A string is printed as it is, a number in its usual form, an object as a shallow listing with a depth limit, and an array as a list in square brackets. A percent sign in the first argument turns the call into a format string: %s takes a string and %d a number.
console.log('a plain string');
console.log(42, 'and a number');
console.log('%s is %d years old', 'JavaScript', 31);
console.log({ name: 'object', value: 7 });
console.log([1, 2, 3]);
node formats.js
a plain string
42 and a number
JavaScript is 31 years old
{ name: 'object', value: 7 }
[ 1, 2, 3 ]
The object is printed by the runtime inspector, not by JSON.stringify: the quotes around the keys are missing and a nested object would be shown one level deep. When the exact text matters, for a file or for a test, build it with a template literal or JSON.stringify; console.log is for people.
Comments
Two comment forms exist. // comments out the rest of the line, and /* ... */ comments out everything between the markers, across lines. A commented-out line of code is still parsed as a comment and never runs, so a block comment may hold a line that would not compile.
// A one-line comment: everything after // is ignored.
/* A block comment
can span several lines. */
console.log('comments do not print'); // a trailing comment
/*
console.log('this line is inside a comment');
*/
node comments.js
comments do not print
The program prints one line, because three of its statements are inside comments. A block comment does not nest: the first */ closes it, whatever lies between. A commented-out block of code that itself contains */ ends the comment early and the rest becomes code again, which is the trap the linter reports as a parse error.
The semicolon rule
The semicolon ends a statement, and a newline usually does the same thing: automatic semicolon insertion adds the missing semicolon when the next token cannot continue the current expression. The rule is reliable in ordinary code and dangerous in exactly one place: a line that starts with (, [, +, - or a backtick can continue the line above.
The program below has two statements without semicolons. The first subtraction starts with a minus and is read as part of the line above, so total is not 10. The second expression starts with a dot, and no rule joins it to the previous line, so it is a new statement and the sum is what it looks like.
const price = 10;
const total = price
- 5;
console.log('total:', total);
const items = [10, 20, 30];
const sum = items
.reduce((acc, value) => acc + value, 0);
console.log('sum:', sum);
node asi.js
total: 5
sum: 60
The lesson keeps the semicolons in every file that follows. A style that leaves them out is not wrong, and a project that does so enforces it with a linter and a formatter; the point here is that the parser has a rule, the rule is not the newline, and a minus sign at the start of a line is where it shows.
Exercises
- Run node streams.js > out.txt. Which lines does the file contain, and which one stayed on the terminal?
- Print the object of formats.js with JSON.stringify and compare the two forms. Which one can be read back by JSON.parse?
- Remove the semicolon after the first line of asi.js and add one after the second. Predict both numbers before you run the file.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
Which call writes to standard error?
Answer
console.error
console.error writes to the second stream, which a program can redirect apart from the results.
-
What does
process.stdout.write("abc")add after the string?Answer
Nothing
write adds no newline, so the next write continues the same line.
-
What does
console.log("%s is %d", "JavaScript", 31)print?Answer
JavaScript is 31
The percent signs in the first argument take the following values in order.
-
How many lines does the comments.js of this lesson print?
Answer
One
Only one console.log is outside a comment; the commented calls never run.
-
What does this program print?
const price = 10; const total = price - 5; console.log(total);Answer
5
The minus sign continues the expression, so the binding holds 10 - 5 and the program prints 5.
-
When does automatic semicolon insertion add a semicolon?
Answer
When the next token cannot continue the current statement
The parser inserts a semicolon only where the next token would otherwise be a syntax error.
-
Which line starts a statement that a newline cannot end safely?
Answer
A line that starts with an opening parenthesis
A line starting with ( can be read as a call on the previous line, which is why some styles start such lines with a semicolon.
-
Do block comments nest in JavaScript?
Answer
No, the first */ closes the comment
The first closing marker ends the block, so a commented-out block that contains one ends early.
-
Where do the diagnostics of
console.errorgo when the command isnode app.js 2>/dev/null?Answer
Nowhere: they are discarded
The redirection sends file descriptor 2 to the null device, and only standard output reaches the terminal.
-
Which statement about console.log is right?
Answer
It separates several arguments with a space
Arguments are joined with a space, and the object form comes from the inspector rather than from JSON.
The code on this page was run with Node.js 24.22.1 on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev