var_dump() for Javascript

PHP, for example, has var_dump() and print_r() to facilitate quick variable dumps to log files, etc.

For Javascript, one can use console.log() in combination with JSON.stringify() to achieve something similar.

  console.log( JSON.stringify(object) );

It’s also possible to “pretty-print” the output like so:

  console.log( JSON.stringify(object, null, 2) );

For more information about JSON.stringify(), check out this page

Leave a Comment

Notify me of followup comments via e-mail. You can also subscribe without commenting.