How to log JavaScript stack traces and objects using console.trace (#tilPost)

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 …


This content originally appeared on Stefan Judis Web Development and was authored by Stefan Judis

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. ?


Reply to Stefan


This content originally appeared on Stefan Judis Web Development and was authored by Stefan Judis


Print Share Comment Cite Upload Translate Updates
APA

Stefan Judis | Sciencx (2021-02-14T23:00:00+00:00) How to log JavaScript stack traces and objects using console.trace (#tilPost). Retrieved from https://www.scien.cx/2021/02/14/how-to-log-javascript-stack-traces-and-objects-using-console-trace-tilpost/

MLA
" » How to log JavaScript stack traces and objects using console.trace (#tilPost)." Stefan Judis | Sciencx - Sunday February 14, 2021, https://www.scien.cx/2021/02/14/how-to-log-javascript-stack-traces-and-objects-using-console-trace-tilpost/
HARVARD
Stefan Judis | Sciencx Sunday February 14, 2021 » How to log JavaScript stack traces and objects using console.trace (#tilPost)., viewed ,<https://www.scien.cx/2021/02/14/how-to-log-javascript-stack-traces-and-objects-using-console-trace-tilpost/>
VANCOUVER
Stefan Judis | Sciencx - » How to log JavaScript stack traces and objects using console.trace (#tilPost). [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/02/14/how-to-log-javascript-stack-traces-and-objects-using-console-trace-tilpost/
CHICAGO
" » How to log JavaScript stack traces and objects using console.trace (#tilPost)." Stefan Judis | Sciencx - Accessed . https://www.scien.cx/2021/02/14/how-to-log-javascript-stack-traces-and-objects-using-console-trace-tilpost/
IEEE
" » How to log JavaScript stack traces and objects using console.trace (#tilPost)." Stefan Judis | Sciencx [Online]. Available: https://www.scien.cx/2021/02/14/how-to-log-javascript-stack-traces-and-objects-using-console-trace-tilpost/. [Accessed: ]
rf:citation
» How to log JavaScript stack traces and objects using console.trace (#tilPost) | Stefan Judis | Sciencx | https://www.scien.cx/2021/02/14/how-to-log-javascript-stack-traces-and-objects-using-console-trace-tilpost/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.