With the console object and its logging methods, long are the days of calling alert() to debug and get a variable’s value.
To know more, talk2us@ideas2it.com or visit www.ideas2it.com
2. console:
With the console object and its logging methods, long
are the days of calling alert() to debug and get a
variable’s value.
Besides the general logging methods that we have
discussed in the previous part, there are few more
methods that we can play around with.
We will cover the below two in this part.
- Stack Tracing
- Asserting
3. Stack Tracing:
The console.trace() method displays a trace that show
how the code ended up at a certain point.
Take a look at the below example, to understand how
console.trace() works.
function hello(name) {
console.trace('name:', name);
return `Hello ${name}!`;
}
hello('JS Nugget');
Output:
4. Asserting:
The console.assert method is an easy way to run
assertion tests. If the assertion of the 1st argument
fails, the subsequent argument gets printed to the
console.
Let’s look at this example,
// this will pass, nothing will be logged
console.assert(1 == '1', '1 not == to "1"');
Output:
// this will pass, nothing will be logged
console.assert(1 === '1', '1 not == to "1"');
The below assertion fails,
5. STAY AROUND FOR
OTHER TRICKS!
That you can play around with console, in the
forthcoming parts.