Open In App

Console in JavaScript

Last Updated : 11 Jul, 2025
Comments
Improve
Suggest changes
29 Likes
Like
Report

The console object provides access to the browser's debugging console (or terminal in Node.js). It is used to log information, debug code, and interact with the runtime environment during development.

Commonly Used console Methods

Here are the most frequently used methods of the console object:

1. console.log()

The console.log() function logs general information to the console. This is one of the most commonly used methods for debugging in JavaScript.

JavaScript
console.log("Hello, World!");

Output
Hello, World!

2. console.error()

The console .error() function in JavaScript Logs error messages to the console. Typically displayed in red. It is used to display an error message on the console.

JavaScript
console.error("This is an error message.");
consoleerror
console .error()

3. console .warn()

The console. warn() function Logs warnings on to the console to warn the user about certain scenarios, typically displayed in yellow.

JavaScript
console.warn("This is a warning message.");
consolewarn
console. warn()

4. console.info()

The console.info() logs informational messages, which usually appear as standard logs but can be styled differently in some environments. Using the %c flag and passing the style object as the second parameter to the function can be used to style the info message.

JavaScript
console.info('%cThis is a styled info message!', 'color: blue; font-size: 16px; font-weight: bold;');
hello
console.info()

5. console .table()

console.table() is a method that displays data in a structured table format, making it easier to read and compare. It takes arrays or objects and organizes their properties or elements into rows and columns, helping with debugging or analyzing large or complex datasets.

JavaScript
console.table([{name: "Amit", age: 30}, {name: "Jatin", age: 25}]);

Output
┌─────────┬─────────┬─────┐
│ (index) │  name   │ age │
├─────────┼─────────┼─────┤
│    0    │ 'Amit'  │ 30  │
│    1    │ 'Jatin' │ 25  │
└─────────┴─────────┴─────┘

6. console .time() & console .timeEnd()

The console.time() method starts a timer with a specified label, and console.timeEnd() stops the timer and logs the elapsed time in milliseconds. These methods are useful for measuring how long a block of code takes to execute.

JavaScript
console.time('timer1');
function loops() {
    for (let i = 0; i <= 10000; i++) {

    }
}
loops()
console.timeEnd('timer1'); 

Output
timer1: 0.278ms

7. console.assert()

console.assert() logs an error message if the given condition is false, helping to catch issues during development. If the condition is true, it produces no output.

JavaScript
console.assert(5 > 10, "This assertion failed");

8. console .group() and console.groupEnd()

The console.group() and console.groupEnd() methods let you group related logs together in the console. This makes it easier to organize and read logs, especially when debugging.

JavaScript
console.group('User Information');
console.log('Name: Ritik');
console.log('Age: 30');
console.groupEnd();

Output
User Information
  Name: Ritik
  Age: 30

9. console.count()

console.count() logs the number of times it has been called with a specific label. It helps track how many times a particular code block or function is executed during runtime.

JavaScript
console.count("countLabel");  
console.count("countLabel"); 
console.count("countLabel");
console.count("countLabel"); 

Output
countLabel: 1
countLabel: 2
countLabel: 3
countLabel: 4

10. console.trace()

The console.trace() method outputs a stack trace to the console, showing the path your code took to reach the point where it was called. This helps track the flow of execution and identify where a function was invoked.

JavaScript
function a() {
    b()
}
function b() {
    c()
}
function c() {
    console.trace()
}
a()
Suggested Quiz
6 Questions
Which console method is primarily used to log general information during development?
  • A
    console.error()
  • B
    console.warn()
  • C
    console.log()
  • D
    console.info()
Explanation:
What does the console.error() method do in JavaScript?
  • A
    Logs warnings to the console
  • B
    Logs informational messages
  • C
    Logs error messages to the console
  • D
    Logs the execution time of code
Explanation:
How does the console.warn() method visually differentiate its output?
  • A
    Displays messages in blue
  • B
    Displays messages in green
  • C
    Displays messages in yellow
  • D
    Displays messages in red
Explanation:
What is the purpose of the console.table() method?
  • A
    To log error messages in a structured format
  • B
    To display data in a structured table format
  • C
    To group related logs together
  • D
    To measure execution time of code
Explanation:
Which console method is used to start and stop a timer for performance measurement?
  • A
    console.time() and console.timeEnd()
  • B
    console.start() and console.stop()
  • C
    console.measure() and console.report()
  • D
    console.track() and console.finish()
Explanation:
What does the console.assert() method do when its condition is false?
  • A
    Logs a success message
  • B
    Logs an error message
  • C
    Produces no output
  • D
    Logs a warning message
Explanation:
Quiz Completed Successfully
Your Score :   2/6
Accuracy :  0%
Login to View Explanation
1/6 1/6 < Previous Next >

Explore