+ 2
Array and Object
Can anyone tell me the difference between an Array and an Object in Javascript Purpose of both to store the values of different data types.Then what is the difference between these two.And which one we use and why?
2 ответов
+ 2
An array is a list of objects. An object is a type that contains some data. We have the basic objects, like integers, strings, floats and then we have clssses.
Classes are templates for storing and manipulating the basic object types mentioned.
As an example, the "document" variable is an instance of a class that contains various variables and methods like "write()" & more.
Hope it's a little clearer :)
+ 2
Samir Singh
I think the main difference is in storing and accessing data
An object looks like this:
----
var object{
key: 'value',
'key2': 'value2'
}
----
where we store data using key-value pairs
And accessing might look like this:
----
object.key; //value
object['key2']; //value2
----
In an array we store the data orderly,
----
var Array = [value,'value2'];
----
And accessing:
----
Array[0]; //value
Array[1]; //value2
----
The data(value) can be of any data type (even an array or object) in both the cases.
You can a better understanding from here - https://dev.to/zac_heisey/objects-vs-arrays-2g0e#:~:text=Arrays,-%23beginners%20%23webdev%20%23&text=Both%20objects%20and%20arrays%20are,store%20a%20list%20of%20values.