Various ways console.<> can be used in Aura or LWC to debug stuffs:
1. console.assert() - The assert() method writes a message to the console if an expression evaluates to false.
Example -
const myObj = {firstname:"John", lastname:"Doe"};
console.assert(x + y == 11, myObj);
2. console.clear() - Clear all messages in the console
Example -
console.clear();
3. console.error() - The error() method writes an error message to the console.
Example -
console.error("You made a mistake");
4. console.info() - The info() method writes a message to the console.
Example -
console.info("Hello world!");
5. console.warn() - The warn() method writes a warning to the console.
Example -
console.warn("This is a warning!");
6. console.table() - The table() method writes a table to the console.
Example -
console.table({firstname:"John", lastname:"Doe"});
7. console.log() - The log() method writes (logs) a message to the console. The log() method is useful for testing purposes.
Example -
console.log('!!!groceriesList!!!',JSON.stringify(cmp.get("v.groceries")));
8.console.time() and console.timeEnd() - The time() method starts a timer in the console view.The time() method allows you to time code for testing purposes.
Example -
console.time();
for (let i = 0; i < 100000; i++) {
// some code
}
console.timeEnd();
9. console.trace() - The trace() method displays a trace that show how the code ended up at a certain point.
Example-
function myFunction() {
myOtherFunction();
}
function myOtherFunction() {
console.trace();
}
10. console.group() and console.groupEnd() - The group() method starts a message group.All new messages will be written inside this group.
Example -
console.log("Hello world!");
console.group();
console.log("Hello again, this time inside a group!");
or
console.log("Hello world!");
console.group();
console.log("Hello again, this time inside a group!");
console.groupEnd();
console.log("and we are back.");
11. console.groupCollapsed() -
The groupCollapsed() method starts a collapsed message group.In the Console, click the expand button to open the new message group.All new messages will now be written inside this group.
Example -
console.log("Hello world!");
console.groupCollapsed();
console.log("Hello again, this time inside a collapsed group!");
11. console.count() -
The count() method counts the number of times console.count() is called.The count() method this number to the console.
Example -
for (let i = 0; i < 5; i++) {
console.count();
}
Visual: https://www.youtube.com/watch?v=A99EgP0hnlw
Reference: https://www.w3schools.com/
Comments
Post a Comment