JavaScript Debugging Console
The browser console is the most important debugging tool for JavaScript beginners.
Console Like a Pro
The browser console is the most important debugging tool for JavaScript beginners.
If you learn how to use the console properly, you will find bugs faster and understand your code better.
How to Open the Console
In Chrome or Edge, open the console like this:
- Right-click anywhere on the page
- Click Inspect
- Click the Console tab
console.log()
The most common console method is console.log().
Use it to print values and see what your program is doing.
Examples
Example Code
javascriptconsole.log("Hello from JavaScript!");console.warn()
Use console.warn() to display warnings.
Warnings tell you something might be wrong, but your code can still run.
Example
Example Code
javascriptconsole.warn("This is a warning!");console.error()
Use console.error() to display error messages.
Errors show that something has failed.
Example
Example Code
javascriptconsole.error("Something went wrong!");Logging Multiple Values
You can log more than one value at once.
Example
Example Code
javascriptlet x = 10;
let y = 5;
console.log("x =", x, "y =", y);Inspecting Objects
The console is very useful for inspecting objects.
Example
Example Code
javascriptlet user = {name: "John", age: 25};
console.log(user);console.table()
Use console.table() to display data in a table format.
Example
Example Code
javascriptlet users = [
{name: "John", age: 25},
{name: "Anna", age: 30}
];
console.table(users);Debugging Tip: Stop Guessing
Beginners often guess what the value is.
Professionals log the value and confirm it.
If your code is not working, do not guess.
Use console.log().
Next: Breakpoints, Watch, Scope
Logging is powerful, but sometimes you need to pause code execution.
Next page: Learn how to use breakpoints, step through code, and watch variables live.
Interactive Code Examples
Examples
javascriptJS Example 2
javascriptconsole.warn("This is a warning!");JS Example 3
javascriptconsole.error("Something went wrong!");JS Example 4
javascriptJS Example 5
javascriptJS Example 6
javascriptlet users = [
{name: "John", age: 25},
{name: "Anna", age: 30}
];
console.table(users);