Skip to content

Try our new Crash Courses!

Buy one of our new Crash Courses, now hosted on Teachable.

Logging to the console

Summary

console.log("This sentence will appear in the browser console");

Details

Logging to the console allows you to print messages to your browser’s console.

You can open your browser’s console by right clicking a web page and selecting Inspect (or Inspect Element, depending on your browser), then clicking on the Console tab.

You can use logging to check your code as you are working. You can use it in combination with conditional statements, for loops, and functions to make sure your code is working or to help debug it when it’s not working.

Exercises

Try the following statements in the console:

console.log('hello world')

The output will be ‘hello world’. You will also see the word undefined in the output. You will see this in later exercises that also use console.log(). This only happens when you use console.log() inside the console. If you use it inside a JS file, it will just print out what’s inside the log statement.

Some of the other exercises in this course will use console.log(), and some won’t. Since you’ll be entering code directly into the console, console.log() will be unnecessary in a lot of these exercises. However, if you wanted to print anything out to the console from a JS file, you’d have to use console.log().

References

The console object on MDN

Back to: JavaScript Reference > JS Basic Syntax