Use values with same types in any array. Changing types (even for scenarios that you think are totally valid for the day) can rain down hell.
Homogeneity of an array refers to using the same types of variables within an array.
The following arrays are homogenous -
|
|
The following arrays are not -
|
|
You may think you have good reasons to include multiple types. For example group together different attributes of earth -
|
|
Arrays are highly ‘iterable’, and when you iterate you take an element and do something. You cannot do that “something” consistently when you use different types. It can lead to unexpected results, errors and frustrations.
For e.g.:
|
|
What should you do instead?
Use consistent types in an array.
If you want to group together stuff, do it in an object. Use arrays to store series of such objects.
|
|