0

JavaScript has a surprisingly confusing distinction between arrays and objects and array-like objects:

var a = [];  // empty array, right?
a.foo = 'bar';  // a is also an object

The problem is that when inspecting variables in the Chrome DevTools console, it will appear that a is only an empty array:

Chrome is confused by objects/arrays

This sort of array/object usage happens with 3rd party code, so I'm looking for a way to show all properties of a variable in Chrome DevTools.

1
  • Arrays are Objects with a special (self–adjusting) length property and handy array methods, that's it. Array methods are mostly generic and can be applied to any Object (though be careful with host objects). Commented Aug 14, 2014 at 22:44

4 Answers 4

2

In modern browsers (IE9+, FF4+, Chrome5+, Opera12+, Safari5+) you can use the built in Object.keys method:

Object.keys(a);

DevTools Screenshot

From MDN:

The Object.keys() method returns an array of a given object's own enumerable properties, in the same order as that provided by a for...in loop (the difference being that a for-in loop enumerates properties in the prototype chain as well).

Sign up to request clarification or add additional context in comments.

Comments

1

Just use console.log

a = []
[]
a.foo = 'bar'
"bar"
a
[]
console.log(a)
[foo: "bar"]                                                              VM451:2
undefined

Comments

1

When you ask to see an array in the console (this is outside of the javascript specification), the console thinks you want to see just the array elements so that's what it shows you. That doesn't mean that there aren't other properties - that's just how Google chose to implement the console.


An array IS an object and has all the capabilities of an object. You can assign it properties just like an object (because it is an object).

An array also has a special property called .length that automatically keeps track of the length of numeric properties assigned to it which gives it the array-type capabilities and a bunch of methods for dealing with the sequential nature of arrays (.push(), .pop(), .slice(), etc...). A plain object does not have that special property .length or any of those array methods. An array may also have some internal optimizations that help it be more efficient when sequential runs of numeric properties are the only properties accessed.


So, you can iterate just the array elements with:

for (var i = 0; i < arr.length; i++) {
    // access arr[i] here
}

or with .forEach():

arr.forEach(function(value, index, array) {
    // use value and index here
})

Or, you can iterate all the properties either with:

var props = Object.keys(arr);
for (var i = 0; i < props.length; i++) {
    // access props[i] here to get the property
    // and arr[props[i]] to get the value of the property
}

or with:

for (var prop in arr) {
    if (arr.hasOwnProperty(prop)) {
        // access arr[prop] here
    }
}

Comments

1

When chrome does its thing where the console output doesn't contain all the information I'm interested in, I've found that I can access that information by wrapping it in an object:

({a:a})

chrome tools screenshot

By the way, the array always was an object, with or without extra properties.

array instanceof object

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.