Computer >> Computer tutorials >  >> Programming >> Javascript

What is the use of .stack property in JavaScript?


The stack property of Error objects offers a trace of which functions were called, in what order, from which line and file, and with what arguments. The stack string proceeds from the most recent calls to earlier ones, leading back to the original global scope call. It is similar to the stack trace in Java.

Example

function a() {
   throw new Error("error");
}
try{
   a()
}
catch(e) {
   console.log(e.stack)
}

Output

This will give the output −

Error: error
   at a (<anonymous>:2:11)
   at <anonymous>:6:5

Note − Stack is a non-standard property and may not be available in all environments.