Array an Object?

Hi everyone,
I hope you all doing good.
I have one question, as they say all Data types are wrapped or treated as Objects, then how come array is also being treated as Objects?
They are not a data Type!
thanks.

Hi,

An array is a data type of type array. In a strongly typed language you even have to specify the types that the array stores. So for example, an array<int> is quite different from an array<char>.

(Note: a common size for an int is 4 bytes and for a char 1 byte. So if you initialize an array<int> of size 10 this would by 40 bytes large whereas an array<char> of size 10 would be 10 bytes in comparison).

More info:

HI, thankyou sir for your response, but I did not understand it.
actually I am new in programming and I am learning JavaScript.

1 Like

Welcome to programming! :slight_smile:

I’m basically saying: an array is a data type (by definition). So then there’s no problem with it being an being an object.

In JavaScript, an object is a standalone entity, with properties and type. Compare it with a cup, for example. A cup is an object, with properties. A cup has a color, a design, weight, a material it is made of, etc. The same way, JavaScript objects can have properties, which define their characteristics.

1 Like

Arrays are also objects, that is what comes up with typeof.

array = []
console.log(typeof array)    //  'object'

JS has a built in object constructor, Array.

console.log(array.constructor)    //  [Function: Array]

That makes it an instance of Array…

console.log(array instanceof Array)    //  true

The Array constructor has a class method, .isArray()

console.log(Array.isArray(array))    //  true

This reply was meant for an hour ago but I got called away.

2 Likes

Thankyou so much sir and thankyou for the explanation. I really appreciate it sir @toastedpitabread

Thankyou so much @mtf sir, No problem I really appreciate you guys taking time for helping me out!

1 Like