Course contentsShow
← JavaScript
Lesson 27 of 2,7231. JavaScript FoundationsFree lesson

Console.log for Debugging and Output

Use console.log() to print values and debug your code in both browser and Node environments.

Console.log for Debugging and Output

What you'll learn: How to use console.log() to print values and see what's happening in your JavaScript code.

The Simplest Way to See What's Happening

Think of console.log() as your window into what JavaScript is doing. Just like you might write notes to yourself while solving a math problem, console.log() lets you print out values so you can see what your code is actually doing at any moment.

How It Works

When you write console.log() and put something inside the parentheses, JavaScript displays that information in the console—whether you're in a browser's developer tools or in the Node.js terminal.

console.log("Hello, World!");
console.log(42);
console.log(5 + 3);

This prints:

Hello, World!
42
8

Why This Matters for Debugging

When your code doesn't work as expected, console.log() becomes your detective tool. You can place it at different points in your code to see what values exist, check if a line of code runs at all, or understand the order things happen in.

In Both Browser and Node

The beauty of console.log() is that it works identically whether you're running JavaScript in a browser console, inside an HTML file with a <script> tag, or executing a JavaScript file with Node. The syntax is exactly the same—only the location where the output appears changes (browser console vs. terminal).

Key Takeaway: console.log() is your primary tool for printing values and understanding what your code is doing—use it freely whenever you need to see what's happening inside your program.