- Example:
const num = 10; console.log("The value of num is:", num);
- Explanation: Used to print messages to the console. It can take multiple arguments and display them with proper formatting.
- Example:
const errorMessage = "Something went wrong!"; console.error(errorMessage);
- Explanation: Outputs an error message to the console, typically in red color, indicating a critical error.
- Example:
const warningMessage = "This action is not recommended."; console.warn(warningMessage);
- Explanation: Outputs a warning message to the console, typically in yellow color, indicating a non-critical issue.
- Example:
const infoMessage = "Information: User logged in successfully."; console.info(infoMessage);
- Explanation: Outputs an informational message to the console, typically in blue color, providing additional information.
- Example:
const debugMessage = "Debugging information..."; console.debug(debugMessage);
- Explanation: Outputs a debug message to the console, typically used for debugging purposes during development.
- Example:
const condition = false; console.assert(condition, "Condition is not true!");
- Explanation: Checks if a specified condition is true. If not, it logs an error message to the console.
- Example:
console.clear();
- Explanation: Clears the console of all previous messages and logs.
- Example:
const users = [ { id: 1, name: "John", age: 30 }, { id: 2, name: "Jane", age: 25 }, ]; console.table(users);
- Explanation: Displays tabular data in a table format in the console, making it easier to read arrays or objects.
- Example:
console.group("Group 1"); console.log("Message 1"); console.log("Message 2"); console.groupEnd();
- Explanation: Groups related log messages together in a collapsible group, making it easier to organize and understand complex logs.
- Example:
console.time("Timer"); // Code to measure execution time console.timeEnd("Timer");
- Explanation: Measures the time it takes to execute a block of code between
console.time()
andconsole.timeEnd()
calls, allowing for performance optimization.
- Example:
const fruits = ["apple", "banana", "apple", "orange"]; fruits.forEach((fruit) => { console.count(fruit); });
- Explanation: Counts the number of times
console.count()
is called with the same label, useful for tracking the frequency of occurrences.
- Example:
function foo() { bar(); } function bar() { console.trace(); } foo();
- Explanation: Outputs a stack trace to the console, showing the function call path that led to the current point of execution. Useful for debugging.
- Example:
const obj = { a: 1, b: 2 }; console.dir(obj);
- Explanation: Outputs a JavaScript representation of the specified object to the console. This can be useful for exploring complex objects in more detail.
- Example:
console.profile("MyProfile"); // Code to profile console.profileEnd("MyProfile");
- Explanation: Initiates and stops the JavaScript profiler under the label provided, allowing developers to analyze the performance profile of a section of code.
- Example:
console.timeStamp("Event occurred");
- Explanation: Adds a timestamp marker to the browser's timeline, which can be useful for tracking events and performance analysis.
- Example:
console.markTimeline("Event occurred");
- Explanation: Marks the timeline with the specified label, which can be useful for tracking events and performance analysis.
- Example:
console.time("Timer"); // Code to measure execution time console.timeLog("Timer", "Intermediate log"); // More code console.timeEnd("Timer");
- Explanation: Logs intermediate messages with a timestamp during the execution of a timer set by
console.time()
.
- Example:
console.log(console.memory);
- Explanation: Provides information about the memory usage of the page, including
jsHeapSizeLimit
,usedJSHeapSize
, andtotalJSHeapSize
.
- Example:
console.log(console.global);
- Explanation: Outputs the global object, which is
window
in web browsers andglobal
in Node.js environments.
- Example:
try { throw new Error("Custom error"); } catch (error) { console.exception(error); }
- Explanation: Logs an exception object to the console, providing detailed information about the error, including the stack trace.
- Example:
console.memory.profile("MemoryProfile"); // Code to profile memory usage console.memory.profileEnd("MemoryProfile");
- Explanation: Initiates and stops the memory profiler under the label provided, allowing developers to analyze memory usage.
- Example:
console.memory.timeline("MemoryEvent");
- Explanation: Marks the timeline with the specified label, capturing memory information at that point, useful for memory profiling.