4

According to Professional Javascript for Web developers array is not a datatype in Javascript:

❑   “undefined” if the value is undefined 
❑   “boolean” if the value is a Boolean 
❑   “string” if the value is a string 
❑   “number” if the value is a number
❑   “object” if the value is an object or null 
❑   “function” if the value is a function

Is that correct?

5 Answers 5

5

This is correct, Array is just a descendant of object. Note though it does override a few things, for example .toString() in an array prints it's members in a comma list, instead of "[Object object]" like a plain object would.

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

4 Comments

But isn't number, boolean and string also objects?
@weng - are you asking which global object types have constructors, or...? In that case it would include your above list Array, Date, RegExp and Error, they are all types defined in ECMAScript, if that's the question...I'm a bit confused on what you're after here.
they don't report their type as Object, no.
@weng: numbers, booleans and strings are primitives, not objects. However, corresponding objects do exist for each (Number, Boolean and String), and they do have type "object". For example: typeof 2 returns "number" while typeof new Number(2) returns "object".
2

I believe that is because an "Array" is an "object"

http://jsfiddle.net/z5Gv2/

1 Comment

I think you can use if(foo instanceof Array){...} to check if it is one.
1

As others say it's treated as "object". You can test for an object being an array by checking if its constructor is === to Array.

Comments

1

yes , Array is not a data type in JavaScript. The set of types in JavaScript are of Primitive values and Objects.

So Array falls under the category of Objects.

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
-1

Arrays are not a data type in Javascript because under the hood they are objects with key:value pairs. Where the keys are the index and values are the array content. For example

let example = ["a","b","c","d","e"]

is the same as an object representation

let example = {
0: "a",
1: "b",
2: "c",
3: "d",
4: "e"
};

1 Comment

While it is true that arrays are objects, and the example object you created can behave in some ways like an array the your two examples are not actually "the same" as you stated. For instance, example.map() would work in your first example, but not in the second example. This is because in the second case, the "example" does not have the Array prototype methods.

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.