Explain Codes LogoExplain Codes Logo

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

javascript
debugging
console-log
json-stringify
Alex KataevbyAlex Kataev·Mar 11, 2025
TLDR

JavaScript equivalent to PHP's var_dump or print_r for quick debugging is console.log(). To inspect an object more in-depth, use console.dir():

console.dir({ name: "Alice", age: 25 }); // "Alice, tell us your secrets!"

For a nicely formatted JSON string in your output, use JSON.stringify():

console.log(JSON.stringify({ name: "Alice", age: 25 }, null, 2)); // "The signed confession, your Honour!"

In-depth Debugging with Custom and Advanced Techniques

Occasionally, a standard console.log() doesn't shed enough light on the grand mystery that is your JavaScript object. Fear not! There are more advanced and specialised techniques in your debugging toolkit:

function debug(obj) { // Because sometimes even URLs don't want to be JSON return JSON.stringify(obj, (key, value) => typeof value === 'function' ? '' + value : value, 2 ); } console.log(debug({ name: "Alice", age: 25, greet: () => "Hello!" })); // "No functions allowed. Only truth!"

Debugging in Browser vs. Debugging in Node.js

Let's not forget about browser devtools, the Poirot of JavaScript debugging. But sometimes you're not in the cozy comforts of your browser environment and you need to step up your game:

Node.js util.inspect:

const util = require('util'); // Talk about going above and beyond! console.log(util.inspect(obj, { showHidden: false, depth: null, colors: true }));

Structured Clone Algorithm:

console.log(structuredClone(obj)); // "Our fancy lie detector!"

Adding Type Differentiation to Your Belt

Beyond just the values, knowing data types is half the battle. Unfortunately, JavaScript's typeof won't make the distinction between null and object. But we have our ways:

function typeVisualization(obj) { for (const [key, value] of Object.entries(obj)) { // "Confess your type!" console.log(`${key} (${typeof value}):`, value); } } typeVisualization({ id: 1, name: null, isDeveloper: true }); // "No more hiding!"

Compatibility and Edge Cases

Creating a universal solution is like trying to solve a Rubik's cube blindfolded. One move might align some squares while throwing others off. Circular references, for instance, can cause JSON.stringify() to throw errors. Luckily, there are solutions like flatted.js or custom replacer functions designed to handle such edge cases.

Writing Humane and Performant Debuggers

Just as you wouldn't handle all criminal cases in a small-town precinct, debugging massive data shouldn't all happen in one console.log(). Lazy-loading properties or pagination can prevent browser slowdowns and allow for organized debugging.

Reading the Manual

Lastly, debugging isn't just about pulling out tools from your kit. Sometimes, it's about learning how to use them effectively. Tools like console.log(), console.dir(), and JSON.stringify() have comprehensive documentation that can offer new insights and functionality: