JavaScript Console
The console is a tool in your browser (or in Node.js) where JavaScript prints messages. It is the first tool for finding mistakes in your code.
Open the console
- Press F12 in Chrome, Firefox or Edge, and click the Console tab.
- Mac: Cmd + Option + J (Chrome).
- You can also type JavaScript directly in the console and press Enter.
console methods
| Method | Use |
|---|---|
console.log() | Normal message |
console.info() | Information |
console.warn() | Warning (yellow) |
console.error() | Error (red) |
console.table() | Shows an array or object as a table |
console.group() | Groups messages together |
console.time() | Measures time |
console.clear() | Clears the console |
console.log("Normal message");
console.warn("This is a warning");
console.error("Something went wrong");
const user = { name: "Robin", age: 25 };
console.log(user);
console.table([{ id: 1, name: "Robin" }, { id: 2, name: "Maria" }]);
The text output looks like this (the console shows warnings in yellow, errors in red, and the table in a grid):
Output of the above program is
Normal message
This is a warning
Something went wrong
{ name: "Robin", age: 25 }
Log many values
let a = 10;
let b = 20;
console.log("Sum:", a + b);
console.log(a, b);
Output of the above program is
Sum: 30
10 20
Note:
Remove the console.log() lines that you used for testing before you publish the website.
Summary
- The console shows messages, warnings and errors.
- Use
console.log()for output andconsole.error()for errors. - Next: JavaScript Variables.
Congratulations! Chapter Finished. Learn more about the similar topics:
| Exercises & Assignments |
|---|
| Write a JavaScript program to display Hello World |
| Interview Questions & Answers |
|---|
| What is JavaScript? |
| What is strict mode in JavaScript? |