JavaScript, webdev

Javascript – Print Stack Trace

Simple and short code to drop a stack trace to your console and/or page.

var e = new Error('dummy');
var stack = e.stack.replace(/^[^\(]+?[\n$]/gm, '')
  .replace(/^\s+at\s+/gm, '')
  .replace(/^Object.\s*\(/gm, '{anonymous}()@')
  .split('\n');
console.log(stack);

and here you can play with a working demo on JSbin


Discover more from Ido Green

Subscribe to get the latest posts sent to your email.

Standard