Which console methods are worth knowing beyond console.log?
There are around twenty console methods, and only a handful of them earn their place in real debugging work.
console.log('a message');
console.info('informational');
console.warn('something looks odd');
console.error('something failed');
console.group('Grouped output');
console.log('inside the group');
console.groupEnd();
// Timed output.
console.time('work');
for (let i = 0; i < 100000; i++) {
// do some work here
}
console.timeEnd('work');
// A table of objects.
console.table([
{ id: 1, name: 'Ada' },
{ id: 2, name: 'Bob' }
]);
// Counters.
console.count('iteration');
console.count('iteration');
// Reports only when the condition is false.
console.assert(false, 'this will be reported');The two methods that save the most time are the table method and the timer methods. The table method renders a list of objects as an aligned table, so you can see all the fields of every row at once instead of scrolling through separate log lines. The timer methods give you a measured duration, which turns a vague claim that something is slow into a number that you can compare before and after a change.
The grouping methods are there to give your output a structure. When one function logs twenty different things, wrapping them in a group with a label turns a wall of output into readable sections.
The count method is surprisingly useful. If you add a count at the top of a branch, you can see at a glance whether that branch runs once or a hundred times, and that answers a surprising number of bugs in a single line of code.
Finally, console.assert reports a message only when the condition is false. It is not a replacement for a real test, but it is a quick way to record something that must always be true while you are exploring your code.