Jump to content

the substitute for var_dump in javascript


hisoka

Recommended Posts

Typeof can tell you what a variable is, but the closest semi-equivalent is probably JSON.stringify()

 

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify

 

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof

 

--edit--

 

I see that console.log() provides the same functionality (to the console) if you separate its parameters with commas.

Link to comment
Share on other sites

"console.log() will show the contents of a variable. You have to have the browser's Javascript console open to see the result"

 

var x = 12 ;

console.log(x); // gives 12 as a result . So console log does not give information about the variable it prints only the variable in the web console of Mozilla Firefox . I need information about the variable :)

Link to comment
Share on other sites

JSON.stringify convert a value to string it does not give information about the variable :

 

JSON.stringify( Boolean(10>9)); // "true" Boolean to string

 

JSON.stringify(9); // "9" integer to string

 

typeof() gives the type of the variable but it does not give enough helpful resources about the variable , for example , in a for loop like var_dump in php does

 

Any other suggestions ?

Link to comment
Share on other sites

You're welcome to write your own function that uses things like typeof, String.length, etc. Maybe someone else wrote one. Most people use console.log. You haven't said what information you need that is missing from the alternatives.

Link to comment
Share on other sites

ok here it is : I wrote it myself

 

var x = "cat";
var g = "abcdeft"
var h = 1;
for( var i=0 ; i<x.length ; i++)
{
var z = g.indexOf(x.substring(i ,i+1));
var h = h+(z*1*i)*(z*i*i);
console.log(h);
}

 

I would like to get information about the i in the var z = g.indexOf(x.substring(i ,i+1)); which is in turn inside the loop . I no need to use typeof as I know i is a number . I am asking if there is a function in javascript that is similar to var_dump in php .

Link to comment
Share on other sites

No, there's not 1 function in Javascript that works like var_dump. Most people use console.log because you can expand and interact with the variable if it's an object or array. Numbers are printed without quotes, that's how you know they're numbers. Anything quoted is a string. Other values like booleans or null or undefined are also not quoted.

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...