The difference of Arrays vs Objects in Javascript

Objects are, according to Merriam-Webster is something material that may be perceived by the senses. While Arrays, are, according to Merriam-Webster is a number of mathematical elements arranged in rows and columns.

In JavaScript, Objects are used to model real-world objects, giving them properties and behavior just like their real-world counterparts while Arrays, according to w3schools.com are used to store multiple values in a single variable.

This is an object :

let dog {
    name: "LeBrown",
    numLegs: 4
}; 

While this is an array:

var cars = ["Volkswagen", "Volvo", "Merc"];

The difference of both are:

1.) Arrays are just objects but with some extra methods.

2.) There is nothing an object can do that an array can’t.

As a general rule of thumb, if you need to store a collection of properties with varying types, use an object. Otherwise, use an array.

 

Reference:
https://frontendmayhem.com/javascript-arrays-objects/
https://www.w3schools.com/js/js_arrays.asp
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/object-oriented-programming/create-a-basic-javascript-object
Advertisement

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.