Published at
Updated at
Reading time
1min
This post is part of my Today I learned series in which I share all my web development learnings.

Today I saw an update to the MDN compat data, and it covered a method available on console. console includes many more useful functions than the commonly used console.log method.

One of them is console.trace that you can use to log JavaScript stack traces.

function someFunction() {
  function anotherFunction() {
    console.trace();
  }

  anotherFunction();
}

somefunction();

// logs:
// anotherFunction @ VM3917:3
// someFunction    @ VM3917:6
// (anonymous)     @ VM4184:1

One thing I learned is that console.trace also accepts multiple arguments so that you can log objects and stack traces in the same call. ๐ŸŽ‰

function someFunction() {
  function anotherFunction() {
    console.trace({foo: "bar"});
  }

  anotherFunction();
}

somefunction();

// logs:
// { foo: "bar" }
// anotherFunction @ VM3917:3
// someFunction    @ VM3917:6
// (anonymous)     @ VM4184:1

If you want to see it in action, here's a quick video. ๐Ÿ‘‡

Example showing the output of console.trace

If you enjoyed this article...

Join 5.5k readers and learn something new every week with Web Weekly.

Web Weekly โ€” Your friendly Web Dev newsletter
Reply to this post and share your thoughts via good old email.
Stefan standing in the park in front of a green background

About Stefan Judis

Frontend nerd with over ten years of experience, freelance dev, "Today I Learned" blogger, conference speaker, and Open Source maintainer.

Related Topics

Related Articles