Imagine a hacker typing away at a keyboard. The computer screen is completely black except for green text.
There are no apps open on the screen - this person doesn't need them. They can talk to the computer in its own language.
That black screen is one example of a console (or terminal) - a program where the user types in commands for the computer, and where they also see the output from the computer.
The Code Editor
The code editor on this page (and the output below it) is one example of a console.
You will type JavaScript into the editor, and you will see the output below.
The computer will follow most commands silently - in other words, it will follow your instructions but it won't display any output.
Logging to the Console
We can use the console.log
command to tell the computer to write (or "log") something to the console.
For example:
console.log(5);
Whatever value you put between the parentheses will be logged to the console.
Notice how console.log
doesn't resolve to a value itself. If the last line of your code is a console.log
statement, the result will be undefined
.
Logging Expressions
Since expressions like 1 + 2
resolve to values (in this case, 3
), you can put expressions between the parentheses as well:
console.log(1 + 2);
Logging Multiple Values
It is also possible to pass multiple values to console.log
:
console.log(2, 4, 6, 8);
All four values above will be logged to the console.
Summary
Earlier we learned that the code editor on this site will display the result of the last expression it executes. Using console.log
allows us to log multiple values and to check a single value over time.