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
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 C
2 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
HTML JavaScript HTML JavaScript is the most popular programming language that helps to add interactivity and provides dynamic behavior. It is known as the client-side scripting language for web pages. JavaScript is used for various purposes, including DOM manipulation, asynchronous requests (AJAX), event handling,
2 min read
JavaScript Glossary JavaScript is one of the most widely used programming languages for web development. This glossary concisely explains key JavaScript terms and concepts, making it easier to understand and reference them.Arrays: Used to store multiple values in a single variable. They are zero-indexed and can hold mi
6 min read