I have studied that an Array has homogenous elements and a List has heterogenous elements. But, in JavaScript, we call it Array and it takes elements of different data type too. How is that possible? Isn't it contradictory to the definition?
-
2What exactly were you studying? Was there a certain book, course, or instructor?Jason Goemaat– Jason Goemaat2021-10-08 22:10:20 +00:00Commented Oct 8, 2021 at 22:10
-
2Arrays are list-like objects in javascript. Always read the docs before asking.pilchard– pilchard2021-10-08 22:12:05 +00:00Commented Oct 8, 2021 at 22:12
-
1Is your question about which kinds of things can go in Arrays? Ie. either all things of the same type, or things of a different type. In JavaScript it's the latter. Addons like TypeScript can greatly aid the type-safety however.Halcyon– Halcyon2021-10-08 22:15:44 +00:00Commented Oct 8, 2021 at 22:15
-
@pilchard I already know it's an object. My question was focus on the literal meaning of the terminology. And, I have read the docs but it doesn't always clear all the doubts.Shah Fahad– Shah Fahad2021-10-08 22:22:15 +00:00Commented Oct 8, 2021 at 22:22
-
@Halcyon Yes, I know the type safety feature of typescript. Thank you for your help anyways.Shah Fahad– Shah Fahad2021-10-08 22:25:16 +00:00Commented Oct 8, 2021 at 22:25
|
Show 3 more comments
1 Answer
Arrays in Javascript are a special kind of object. It's basically an object with numeric keys that are ordered (starting from 0). Also, this object has special methods for dealing with this kind of data structure.
Therefore, an object in Javascript follows the category of dictionary data structure:
In JavaScript, an object is a collection of key-value pairs. This data structure is also called map, dictionary or hash-table in other programming languages.
https://www.freecodecamp.org/news/data-structures-in-javascript-with-examples/#objects-hash-tables-