- Example:
console.assert(2 + 2 === 5, "Error: Math is broken!");
- Why use: Checks if an expression is true, and if not, logs an error message to the console.
- Example:
console.clear();
- Why use: Clears the console.
- Example:
const fruits = ['apple', 'banana', 'apple', 'orange', 'banana']; fruits.forEach(fruit => { console.count(fruit); });
- Why use: Counts the number of times this particular call to
count()
has been called.
- Example:
const obj = {a: 1, b: 2}; console.dir(obj);
- Why use: Prints an interactive listing of all properties of the object.
- Example:
console.error("This is an error message");
- Why use: Outputs an error message to the console.
- Example:
console.group("Group"); console.log("Hello"); console.log("World"); console.groupEnd();
- Why use: Groups related log messages together.
- Example:
console.groupCollapsed("Collapsed Group"); console.log("Hello"); console.log("World"); console.groupEnd();
- Why use: Groups related log messages together, but collapsed by default.
- Example:
console.info("Information message");
- Why use: Outputs an informational message to the console.
- Example:
console.time("Timer"); // Code to measure time console.timeEnd("Timer");
- Why use: Starts a timer you can use to track how long an operation takes.
- Example:
console.table([{a: 1, b: 2}, {a: 3, b: 4}]);
- Why use: Displays tabular data as a table.
- Example:
console.log("Hello, world!");
- Why use: Outputs a message to the console.
- Example:
console.profile("Profile"); // Code to profile console.profileEnd("Profile");
- Why use: Starts a JavaScript CPU profile with the given label.
- Example:
console.time("Timer"); // Code to measure time console.timeEnd("Timer");
- Why use: Stops a timer that was previously started by
console.time()
.
- Example:
console.warn("This is a warning message");
- Why use: Outputs a warning message to the console.
- Example:
function foo() { function bar() { console.trace(); } bar(); } foo();
- Why use: Prints a stack trace to the console.
- Example:
const data = [ { name: 'John', age: 30 }, { name: 'Jane', age: 25 }, { name: 'Alice', age: 35 } ]; console.table(data);
- Why use: Displays array-like or iterable object data in tabular format, making it easier to read and analyze structured data.
- Example:
const obj = { a: 1, b: 2, c: 3 }; console.log(Object.values(obj)); // Output: [1, 2, 3]
- Why use: Outputs an array containing the values of the enumerable properties of an object, in the same order as provided by a for...in loop.
- Example:
console.write("Hello, "); console.write("world!");
- Why use: Writes a message to the console without a newline character, allowing for inline logging or printing.
- Example:
console.xsltProcessor(); // Deprecated and not commonly used
- Why use: Used to create an XSLTProcessor object for transforming XML documents using XSLT stylesheets. However, this method is deprecated and not recommended for use in modern JavaScript development.
Some console methods are less commonly used or even deprecated, like xsltProcessor()
in this case. It's important to stay updated with the latest best practices and recommendations in JavaScript development to ensure efficient and effective debugging and logging.