When to Use Arrays. When to use Objects. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

When to Use Arrays. When to use Objects.

JavaScript does not support associative arrays.You should use objects when you want the element names to be strings (text).You should use arrays when you want the element names to be numbers.

24th Nov 2016, 7:43 AM
MerzaT
MerzaT - avatar
3 Answers
+ 2
hi Alex js does not support associative arrays..If you use a named index, JavaScript will redefine the array to a standard object. After that, all array methods and properties will produce incorrect results... you can test it...
2nd Dec 2016, 4:53 PM
MerzaT
MerzaT - avatar
+ 1
Actually js *does* support associative arrays. Search documentation for Map object. Just keep in mind this is ES2015 object, although you should already write your code in ES2015 specification.
24th Nov 2016, 4:28 PM
Alex B
Alex B - avatar
+ 1
I think of arrays as a type of data entry in a table. All the information contained in an array relates to the name of a row header (or in this case the name of your array). It holds one type of information that comes from many different objects. But, an object is one thing you create with many attributes. Where an array stores all ages of employees, each individual employee is an object who has more attributes than just age. They have gender, weight, height, personality, skills, etc. Each of those attributes would be like the properties of an object.
2nd Dec 2016, 3:27 PM
Sandi Jo Junker
Sandi Jo Junker - avatar