DEV Community

Cover image for JavaScript Arrays vs Objects
Asif Zubayer Palak
Asif Zubayer Palak

Posted on • Edited on

JavaScript Arrays vs Objects

Know the difference between JavaScript Arrays and Objects - and know when to use them.


Difference:

Arrays use numbered indexes
Objects use named indexes

When to use:

Use Arrays when you want the keys to be numbers.
Use Objects when you want the keys to be strings.


How to use Arrays

const pets = ["cats","hamsters","dogs"];
Enter fullscreen mode Exit fullscreen mode

here you are directly assigning the elements in the array.

you can access the elements in the array by

pets[0];
pets[1];
...
pets[pets.length-1];
Enter fullscreen mode Exit fullscreen mode

the starting index is always 0 and ends at array.length-1.

In case of an empty arrays:

const arr = new Array();
const arr = [];
Enter fullscreen mode Exit fullscreen mode

where either of the above can be used to make a new empty array.
In order to add to the array, simply arr[index] = value or arr.push(value);

const arr = new Array(size);
Enter fullscreen mode Exit fullscreen mode

can be used to create an empty array of the size defined.


How to use objects

const pet = {name:"Snuffle", lastName:"Chungus", age:69};
Enter fullscreen mode Exit fullscreen mode

The line above demonstrates how you can create a key-value pair. This keys can be used later to get the value.

const pet = [];
pet["name"] = "Snuffle";
pet["lastName"] = "Chungus";
pet["age"] = 69;
Enter fullscreen mode Exit fullscreen mode

The codeblock above shows how key-value pairs are added to an empty object named pet.

Image of Datadog

Master Mobile Monitoring for iOS Apps

Monitor your app’s health with real-time insights into crash-free rates, start times, and more. Optimize performance and prevent user churn by addressing critical issues like app hangs, and ANRs. Learn how to keep your iOS app running smoothly across all devices by downloading this eBook.

Get The eBook

Top comments (4)

Collapse
 
izio38 profile image
izio38

First, thanks for sharing things.
But, be precise, especially when you target beginners.

element names : keys, or accessor

There is no such undefined size. The size is defined and it's 0.

Variable pet is an array, and you talk about object.
Also, you are assigning a property on person and not on pet

Collapse
 
acesif profile image
Asif Zubayer Palak

Thank you for your valuable input, I'll update the article with your suggestions in mind <3

Collapse
 
jonrandy profile image
Jon Randy 🎖️ • Edited

Object keys can be symbols as well as strings. With some magic, it is also possible to use arrays as keys to access object properties.

Collapse
 
acesif profile image
Asif Zubayer Palak

Amazing! Didn't know this was possible, thanks for sharing the article - learnt something new today!

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

Best practices for optimal infrastructure performance with Magento

Running a Magento store? Struggling with performance bottlenecks? Join us and get actionable insights and real-world strategies to keep your store fast and reliable.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️