How can I get a stack trace for a JavaScript exception?


To get a JavaScript stack trace, simply add the following in your code. It will display the stack trace −

Example

 Live Demo

<html>
   <head>
      <script>
         function stackTrace() {
            var err = new Error();
            return err.stack;
         }

         console.log(stackTrace());
         document.write(stackTrace());
      </script>
   </head>
   <body>
      <p>Stacktrace prints above.</p>
   </body>
</html>

Output


Updated on: 23-Jun-2020

323 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements