Skip to main content

How to Print Output with console.log in Node.js

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

console.log is how a Node.js program talks to you in the terminal: it prints whatever you give it, on its own line. Strings, numbers, arrays and objects all work, and you can pass several at once.

Syntax​

Syntax
console.log(<value>, <value>, ...);
PartWhat it does
console.logPrints to standard output, followed by a new line.
<value>What to print. Several values are printed on one line, separated by spaces.

The Node.js console documentation: "Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3)."

The Example​

The example prints three things: a sentence, a label with a number, and an object that holds an array.

Prerequisites​

  • Node.js. This guide uses Node.js 26.13.1.

Step 1: Write the Code​

Create a file named main.js:

main.js, which prints Hello from Node.js, then Total: with the number 42, then an object with the name Ada and the languages JavaScript and Python.

main.js
console.log('Hello from Node.js');
console.log('Total:', 42);
console.log({ name: 'Ada', languages: ['JavaScript', 'Python'] });

Step 2: Run It​

Terminal
node main.js
Output
Hello from Node.js
Total: 42
{ name: 'Ada', languages: [ 'JavaScript', 'Python' ] }

A zsh terminal where node main.js prints Hello from Node.js, Total: 42 and the object with name Ada and the languages JavaScript and Python.

  • The second line shows two values joined by a space.
  • The object came out formatted, with its strings in quotes, so you can see what's in it.

The command is the same in zsh, bash and PowerShell.

Placeholders​

When the first value is a string with placeholders, the values after it fill them in order: %s for a string and %d for a number.

Placeholders
console.log('%s is %d years old', 'Ada', 36); // Ada is 36 years old

Common Mistakes​

MistakeWhat happensDo this instead
Printing an object nested more than two levels deepThe deepest part shows as [Object]: { a: { b: { c: [Object] } } }Use console.dir(value, { depth: null })
Printing errors and warnings with console.logThey go to standard output, mixed with the resultsUse console.error for problems
Joining an object into a string with +You get [object Object]Pass the object as its own argument

Troubleshooting​

SymptomLikely causeFix
[Object] or [Array] in the outputThe value is deeper than Node.js prints by defaultconsole.dir(value, { depth: null }) prints all of it
[object Object] in a sentenceThe object was turned into a stringPrint it separately or with JSON.stringify(value)

FAQ​

What does console.log return?​

undefined. It only prints, so there's nothing to keep.

Where does the output go when I redirect it?​

console.log writes to standard output, so node main.js > out.txt puts its lines in the file.

Conclusion​

console.log prints any value on its own line, joins several values with spaces, formats objects so you can read them and fills %s and %d placeholders.

Additional Resources​