Welcome to my recap of Day 9 of the free JavaScript 30 online video course by Wes Bos. In case you really have not yet heard of the course, quickly head over to it and get started right away! 🙂

Please refer to the JavaScript 30 archive for all recap posts.

Objective…?

Day 9 is no challenge, but rather some sort of Dev Tools Gospel. 😀

The provided starter files include not more than a single HTML file.

Using the Developer Tools

The whole video is about Wes explaining (new) things you can do with the developer tools in your browser.

I am using Firefox—version 51.0.1, in case you want to know—so this is where the screenshots are from and the descriptions relate to.

Inspector

Wes starts off with a simple and yet cool feature of the Inspector in Chrome.

Tracing the Source of Element Manipulation

Maybe you know this: you visit a website—possibly your own—and you see the effect of some JavaScript running. But you just don’t know what is causing it. Using the Inspector in the Chrome dev tools, you can select any element in the HTML and break on attribute modification, for example. This results in a debugger-like pause exactly where any of the respective element attributes is about to be modified.

Unfortunately, Firefox does not have such a feature. But that’s no problem at all, because the Inspector in Firefox directly informs about any event listener attached to an element. Yay!

Inspector Event Listener
Expanded click event listener displayed in the Firefox Inspector.

Console

The major part of the video then is all about the console.

Logging

OK, this is no mystery: you can log stuff in your console. 🙂 For example, like so:

console.log( 'message' );
console.log()
Logging a message in the console.

Interpolating Data in Messages

You can also log a message with placeholders for arbitrary data:

console.log( 'message with a %s', 'placeholder' );
console.log() with placeholders
Logging a message with a placeholder in the console.

Styling Messages

A special kind of formatting specifier is %c, and it allows to apply CSS to the message to be logged, like this:

console.log( '%cmessage with style', 'color: red; font-size: 24px; font-weight: 600' );
console.log() with styling
Logging a styled message in the console.

Warnings

You can log a warning message:

console.warning( 'warning message' );
console.warn()
Logging a warning in the console.

Error Messages

Besides regular messages and warnings, there is also error messages:

console.error( 'error message' );
console.error()
Logging an error in the console.

As you can see, Firefox does not include a stack trace.

Info Messages

Last but not least, you can also log some info message:

console.info( 'info message' );
console.info()
Logging some info in the console.

Assertions

In case you want to log an error message only if some condition fails, you can use assertions. Here is a stupid example:

console.assert( false, 'expected false to be true' );
console.assert()
Logging a conditional error message by means of an assertion.

Clearing the Console

If you ever want to get rid of everything that lives in your console, you can clear it:

console.clear();

Wes thinks this is something that every experienced developer should do at the very bottom of your script files. 😉

In my opinion, real experts should rather do setTimeout( console.clear, 1000 );. 😀

Viewing DOM Elements

console.log() is able to process almost anything, so you can also log a DOM element:

console.log( document.querySelector( 'p' ) );
console.log() a DOM element
Logging a DOM element in the console.

As you can see, this result in the DOM Element itself being logged, and you can inspect it just like from the Inspector.

But there is another way to dump a DOM element in the console, console.dir(), and it lets you examine all the things that live on the element. Like so:

console.dir( document.querySelector( 'p' ) );
console.dir()
Displaying the full representation of a DOM element JavaScript object in the console.

Grouping Messages

It is should be possible to group output in the console, and such a group may either be expanded or collapsed by default.

dogs.forEach( ( dog ) => {
    console.groupCollapsed( dog.name );
    console.log( `This is ${dog.name}.` );
    console.log( `${dog.name} is ${dog.age} years old.` );
    console.log( `${dog.name} is ${Math.min( 2, dog.age ) * 10.5 + Math.max( 0, dog.age - 2 ) * 4} years old.` );
    console.groupEnd( dog.name );
} );

console.group[Collapsed]()
Grouping output in the console.
Unfortunately, Firefox does not play nice here. It renders the ID or name or the group and the whole group is indented, but you cannot collapse a group. 🙁

Counting

You can have as many independent counters as you can think of valid IDs. 🙂

console.count( 'counter' );
console.count( 'counter' );
console.count( 'counter' );
console.count()
Counting in the console.

This can be useful when debugging something. If you want to know how often something gets called, iterated etc. just increment a dedicated counter, and you don’t need a real debugger at all.

Timing

It is also possible to time the execution of a particular function or task.

console.time( 'fetching' );
setTimeout( () => console.timeEnd( 'fetching' ), 1234 );
console.time() and console.timeEnd()
Timing the execution of some function or task in the console. FYI: “Timer gestartet” is German for “timer started”. 🙂

Tables

Last but not least, you can also display any tabular data in a real table layout:

console.table( dogs );
console.table()
Displaying tabular data in a table layout in the console.

Live Demo

Want to see this in action? Well, here is a live demo. 🙂

Make sure to have a look at your console, though. 😉

Retrospection

There are quite a few differences in how browsers implement the individual features. The event listener thing in the Firefox Inspector is really cool. Not having a stack trace in warnings, and the poor implementation of the grouping functionality, however, are not so cool.

I knew about the different message types when logging, but not about formatting (i.e., placeholders and styling). As of ES6, you don’t really need formatting specifiers anymore—at least not for scalar values—and also, template strings allow for running arbitrary JavaScript, not just referencing a variable.

 

Also, a dog year is not worth seven years. While this is popular belief, it is a misbelief. The first two years in the life of a dog are worth 10.5 years each. Every other year counts as four years. 🙂

And You?

JavaScript 30 is free! So, unless you already did this on your own, what’s your reason not to? 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *