Quoting Mozilla JS Reference on this:
Arrays are list-like objects whose prototype has methods to perform traversal and mutation operations. Neither the length of a JavaScript array nor the types of its elements are fixed.
More on this can be found here
Answer from Ehimah Obuse on Stack OverflowBelow code is an array and there two different data types are stored in. One is Integers and second is String. Is this array is data structure? If yes, why its not same data type.
const hex = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "A", "B", "C", "D", "E", "F"];
Please explain why Array in JavaScript can store multiple data types.
jquery - Array with different data types in javascript - Stack Overflow
In what situation would storing in an array different data types be useful in Javascript? - Stack Overflow
How javascript is able to have different data types in list whereas array in c can't have that
Why Javascript returns "Array" Data structure as Object. Please explain why Array in JavaScript can store multiple data types.
This is the case in any language that is not strongly typed. Your array members can be of different primitive and they also can be objects. In most cases you wouldn't want to use this because there is no clear structure to your array. You would rather have something like:
var data = {
prop: 12,
otherProp: 24.5,
stringProp: "hello",
boolProp: true
};
Although it is possible to store different data types in array, it is considered a poor programming style, because an Array by definition is homogeneous data structure.
How would one handle in uniform way such array:
[ 2.5 , [ 1, 2 ], ["a", "b", "c"], {min: 2, max: 5} ]
for
- sorting
- serialization
- memory utilization
- algorithms i.e. for maximum value
It does not seem natural to have different types in array, does it?