简体   繁体   中英

Array is not a data type in Javascript?

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?

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.

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

http://jsfiddle.net/z5Gv2/

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.

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"
};

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM