AI Hints
Home›JavaScript Tutorial›Getting Started
JavaScript · Lesson 3 of 138

console.log

Printing output.

⏱ 2 min read▶ Video at 2:01🎓 Free
Watch this lesson in the JavaScript courseWatch on YouTube ↗Full JavaScript course

Console dot log is how a program talks to you. You'll use it constantly, to see results, and to find bugs. It can print text, numbers, and several things at once.

console.log
From the video: console.log
  • How a program talks to you
  • See results, find bugs
  • Text, numbers, many things

Printing different things

Text goes in quotes. Numbers don't need quotes, and JavaScript can do maths right inside the brackets. Separate several values with commas, and they're printed with spaces between them. Run it.

app.jsjavascript
console.log("I am learning JavaScript");
console.log(42);
console.log(10 + 5);
console.log("Score:", 90, "points");
Output
I am learning JavaScript
42
15
Score: 90 points

Quotes matter

Anything in quotes is text, exactly as written. So console dot log of ten plus five, in quotes, prints the characters ten plus five. Without quotes, it prints fifteen. Double quotes and single quotes both work. Just start and end with the same kind.

keyvalue
"10 + 5"prints 10 + 5
10 + 5prints 15
'single'also works
Matchstart and end the same

Printing several values

Console dot log can print many values at once, separated by commas, and it puts a space between them. It can print the result of a calculation, and even a list of values in square brackets. Console dot log with nothing inside prints an empty line, which is handy for spacing out your output. Run it.

app.jsjavascript
console.log("Apples:", 3, "Pears:", 5);
console.log("Total fruit:", 3 + 5);
console.log();
console.log(["red", "green", "blue"]);
console.log("Done!");
Output
Apples: 3 Pears: 5
Total fruit: 8

[ 'red', 'green', 'blue' ]
Done!

Summary

Console dot log prints text, numbers, and calculations. Commas separate several values. Next, comments and statements.

Key takeaways
  • console.log(...)
  • Quotes for text
  • Commas for several
Next up: Comments and Statements — notes and semicolons