Formatting

You don’t need ES6 to format a console string. They come with built in formatters.

%o and %O will format an Object.

%d and %i will format numbers.

%s will format a string.

%f will format a floating point number.

You can use them like this:

A nice part of this method is the object is still expandable in browsers that support pretty-printing/expanding/collapsing of objects in the console.

A Final Word — Some “Gotcha’s!”

If you use Node.js, be very wary of the console. It prints to stdout synchronously, which could harm performance.

However, in some browser environments, some console statements are asynchronous, so using console.log will take a snapshot of any variables at the time it outputs, rather than when it is actually first invoked. This means you may not be seeing what you expect. In this case you can use console.dir to make sure you are seeing what you expect.

For that reason and others I’d also caution against using console statements too often in a web browser environment. Typically you can use a debugger statement or breakpoints in the browser for anything you want to do with a console statement. And with a debugger or breakpoint you are able to investigate your variables without having to worry about async problems.