You can use special formatters in the console.log
function to format logs just the way you want.
- Use
%s
to format a variable as a string. - Use
%d
to format a variable as an integer. - Use
%f
to format a variable as a float. - Use
%o
to print a DOM element variable. - Use
%O
to print an object variable.
As a bonus, the %c
formatter can also be used to style console messages with CSS properties!
var el = document.body;
console.log('%c There are %d elements in %O', 'color:lime;background:black;', el.childElementCount, el);