The console
object in JavaScript is way more than just console.log()
. Let's explore some powerful tricks every dev should knowβwith actual outputs!
1. π¦ Different Types of Logs
console.log("General log");
console.info("Informational message");
console.warn("Warning alert");
console.error("Something went wrong!");
π₯οΈ Output:
- β
console.log
: Regular white text - βΉοΈ
console.info
: Blue info icon (browser-dependent) - β οΈ
console.warn
: Yellow warning with a triangle icon - β
console.error
: Red error with a cross icon
2. π console.table()
const users = [
{ name: "Prachi", role: "Dev" },
{ name: "Raj", role: "Tester" }
];
console.table(users);
π₯οΈ Output:
(index) | name | role |
---|---|---|
0 | Prachi | Dev |
1 | Raj | Tester |
So clean and readable!
3. β±οΈ console.time()
+ console.timeEnd()
console.time("loadData");
for (let i = 0; i < 1000000; i++) {}
console.timeEnd("loadData");
π₯οΈ Output:
loadData: 4.56ms
(The number will vary depending on performance.)
4. π§ console.trace()
function first() {
second();
}
function second() {
third();
}
function third() {
console.trace("Stack trace");
}
first();
π₯οΈ Output:
Stack trace
at third (...)
at second (...)
at first (...)
Gives you the full call stack to debug call flow π
5. π console.group()
+ console.groupEnd()
console.group("User Details");
console.log("Name: Prachi");
console.log("Role: Backend Dev");
console.groupEnd();
π₯οΈ Output:
βΆ User Details
Name: Prachi
Role: Backend Dev
(Collapsible in browser console!)
6. π§ͺ console.assert()
const isLoggedIn = false;
console.assert(isLoggedIn, "β User is not logged in!");
π₯οΈ Output:
Assertion failed: β User is not logged in!
(No output if the condition is true.)
7. π§Ό console.clear()
console.clear();
π₯οΈ Output:
π Console is instantly cleared (poof! β¨)
π Final Words
The console isn't just for dumping dataβit's a powerful tool to help you debug smarter, not harder. Try these tricks in your next project, and you'll feel like a true console wizard π§ββοΈ
Top comments (0)