Essential JavaScript
Introduction
JavaScript Basics
Numbers
Strings
Logic
Variables
Loops
Objects
Arrays
Going Further
Essential JavaScript
Introduction
JavaScript Basics
Numbers
Strings
Logic
Variables
Loops
Objects
Arrays
Going Further
The Console
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()
has parentheses behind it. This is important. If all you put in the editor is console.log
, nothing will show up in the console. You have to include the parentheses, and whatever you want to log must be between the parentheses.
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
by separating them with commas:
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.
Learning Goals
I know what a console or terminal is in programming
I can log output to the console with JavaScript
Code Editor
Click "Run Code" to execute your JavaScript in a secure sandbox and see the output below.
Console Output
0 output lines