1

I have been reading that in Javascript :

  • Everything is an object (except primitives such as number,string, boolean, null & undefined)
  • Objects can be treated as associative arrays.

From what I conclude, Array.isArray() should return true for everything (with primitive exceptions aforementioned). What wrong did I understand ?

Thanks !

2
  • “Objects can be treated as associative arrays” does not mean “Objects are associative arrays”. Commented Aug 22, 2012 at 5:28
  • "Objects can be treated as associative arrays" depending on just how you want to define "associative array". There is an obvious similarity, but "associative arrays" in some languages have certain characteristics that JS objects don't have (and vice versa). Commented Aug 22, 2012 at 6:29

2 Answers 2

2

There's a difference between an Object and an Array. An Array is actually an Array Object. It has different methods than other objects. A String is a String object. However, you can access objects as if they were an array. Take the following object

var obj = {
    value1: 'Some Value',
    value2: 'Some Other Value'
    };

You can get values like

obj['value1'];
obj['value2'];

This doesn't mean that it's an Array Object, it just has a different way of accessing it

isArray() checks if the object is an Array Object, not if you can access it like an array

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

Comments

1

Array (a [[Class]]-type in javascript) is not the same as Associative Array (abstract data type), Array.isArray checks if the object is a normal array, as in ({}).toString.call( obj ) === "[object Array]".

  1. If Type(arg) is not Object, return false.
  2. If the value of the [[Class]] internal property of arg is "Array", then return true.
  3. Return false.

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.