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!");
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.");

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.");

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;');

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');
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();
OutputUser 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");
OutputcountLabel: 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()
Similar Reads
Functions in JavaScript
Functions in JavaScript are reusable blocks of code designed to perform specific tasks. They allow you to organize, reuse, and modularize code. It can take inputs, perform actions, and return outputs. [GFGTABS] JavaScript function sum(x, y) { return x + y; } console.log(sum(6, 9)); [/GFGTABS]Output1
5 min read
Code Golfing in JavaScript
Code Golf in JavaScript refers to attempting a problem to solve using the least amount of characters possible. Like in Golf, the low score wins, the fewest amount of characters "wins". JavaScript is a fantastic language for code golfing due to backward compatibility, quirks, it is being a high-level
4 min read
JavaScript Comments
Comments help explain code (they are not executed and hence do not have any logic implementation). We can also use them to temporarily disable parts of your code. 1. Single Line CommentsA single-line comment in JavaScript is denoted by two forward slashes (//), [GFGTABS] JavaScript // A single line
2 min read
JavaScript Function() Constructor
The JavaScript Function() constructor is used to create new function objects dynamically. By using the Function() constructor with the new operator, developers can define functions on the fly, passing the function body as a string. This allows for greater flexibility in situations where functions ne
2 min read
JavaScript console.log() Method
The console.log() method in JavaScript logs messages or data to the console. The console.log() method is useful for debugging or testing purposes. Syntax:console.log("");Parameters: Any message either number, string, array object, etc. Return value: It returns the value of the parameter given. Using
2 min read
Control Statements in JavaScript
JavaScript control statement is used to control the execution of a program based on a specific condition. If the condition meets then a particular block of action will be executed otherwise it will execute another block of action that satisfies that particular condition. Types of Control Statements
3 min read
JavaScript Basics
JavaScript is a versatile, lightweight scripting language widely used in web development. It can be utilized for both client-side and server-side development, making it essential for modern web applications. Known as the scripting language for web pages, JavaScript supports variables, data types, op
6 min read
Debugging in JavaScript
Debugging is the process of testing, finding, and reducing bugs (errors) in computer programs. It involves: Identifying errors (syntax, runtime, or logical errors).Using debugging tools to analyze code execution.Implementing fixes and verifying correctness.Types of Errors in JavaScriptSyntax Errors:
4 min read
JavaScript Events
JavaScript Events are actions or occurrences that happen in the browser. They can be triggered by various user interactions or by the browser itself. [GFGTABS] HTML <html> <script> function myFun() { document.getElementById( "gfg").innerHTML = "GeeksforGeeks"; } </
3 min read
JavaScript get Function
JavaScript get function is used to access the properties of an object using dot notation or square brackets. It allows you to retrieve the value associated with a particular property key and the get function is often used when working with objects that implement JavaScript's getter function. The get
3 min read