You understand what console.log
is? That lets you log arbitrary things to the console. There are a load of other functions as well that let you log specific things to the console that you can use instead – for example if you have an object, you can use console.table(thatObject)
to print the object in a formatted table in the console.
console.trace()
is one of those functions, it prints a stack trace. If you put it in a function, it will print the call stack in the console when the function runs.
Re Errors, when JS code throws an error, there is an Error object which has certain properties. eg message
which is the message that gets printed to the console (eg “Error: undefined is not an object”). The Error object also has a stack
property that shows the call stack at the point of the error. In the console, Errors show red, and there’s normally a little arrow you can click which expands the error shows the properties, including the stack.