JavaScript Console Commands You Should Know
console.log()- The Basics- Prints a message to the console
let name = "Osman"; console.log(name); // Osman
- Prints a message to the console
console.error()- Highlighting Errors- Displays an error message in the console
console.error("Something went wrong"); // Displays the error message with a red icon
- Displays an error message in the console
console.warn()- Highlighting Warnings- Displays a warning message in the console
console.warn("Something is not right"); // Displays the warning message with a yellow icon
- Displays a warning message in the console
console.info()- Highlighting Informations- Displays an informational message in the console
console.info("Here's some useful information"); // Displays the informational message with a blue icon
- Displays an informational message in the console
console.table()- Tabular Data Display- Displays an array in a table format
let users = [ {name: Osman, age: 25}, {name: Ali, age: 30} ]; console.table(users); // Displays the users in a table format
- Displays an array in a table format
console.group()andconsole.group.end()- **Organizing **Logs- Groups related logging statements together
console.group("User Details"); console.log("Name: Osman"); console.log("Age: 25"); console.groupEnd(); console.group("Purchase Details"); console.log("Product: Laptop"); console.log("Price: $1000"); console.groupEnd();
- Groups related logging statements together
console.assert()- Conditional Logging- Logs a message if the provided assertion is false
let age = 15; console.assert(age >= 18, "User is not an adult!"); // This will log an error message since age is less than 18
- Logs a message if the provided assertion is false
console.clear()- Cleaning Up- Clears the console
console.clear(); // This will clear all previous logs in the console
- Clears the console