1

Let's assume i have JS code like this:

var foo = new Array('foo', 'bar'); var bar = new Array(); bar.push(foo); console.log(bar);

The console log only gives me:

Array [ Array[2] ]

I am looking for way to get true log in console. In this case array with subarrays and so on. Something similar to PHP:

echo '<pre>' . print_r($bar, TRUE);

3

3 Answers 3

1

Use dir instead of log. It gives an interactive view.

0

Chrome/Opera/Firefox all allow for further inspection of arrays via console.log().

Try Firebug also for additional debugging ability.

0

If your browser supports it, you can also use the JSON.stringify() function. It's best explained with an example:

var a = [ 
  "123", 
  { "foo": "bar" }, 
  [ "inner", "array", [ "inner-inner", "array-array" ] ] 
];
console.log( JSON.stringify(a) );
// "["123",{"foo":"bar"},["inner","array",["inner-inner","array-array"]]]"

Basically the function converts objects into a JSON string format allowing you to see each object/array and all of it's sub-elements in a plan, string-like format.

Keep in mind that this method will only be effective if the object you are trying to view only contains simple types of data... Function definitions within objects and more complex built-in objects such as window or document will not yield useful information.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.