javascriptobjectdumpvar-dump

What is the JavaScript equivalent of var_dump or print_r in PHP?


I would like to see the structure of object in JavaScript (for debugging). Is there anything similar to var_dump in PHP?


Solution

  • Most modern browsers have a console in their developer tools, useful for this sort of debugging.

    console.log(myvar);
    

    Then you will get a nicely mapped out interface of the object/whatever in the console.

    Check out the console documentation for more details.

    However, if you wish for something similar to a var_dump output you can use the following:

    let myVar = { name: "John", age: 30 };
    console.log(JSON.stringify(myVar, null, 2));  // pretty-print the object