The bizarre quirk of JavaScript arrays (that it is best to by no means use)

For those who’ve finished any sort validation in JavaScript, you’ve got most likely observed that arrays haven’t got their very own sort.

typeof [] === 'array';
// false

typeof [];
// 'object'
Enter fullscreen mode

Exit fullscreen mode

To examine whether or not a variable is an array, you’d have to make use of Array.isArray:

// true
Enter fullscreen mode

Exit fullscreen mode

However why do not arrays have their very own sort? What are the implications of arrays truly being objects?

Arrays are simply objects

In JavaScript, arrays are simply particular objects that use numerical keys and have their very own strategies.

Due to this, you should utilize strategies from the Object class on arrays.

let a = [1, 2, 3];

// [ '0', '1', '2' ]

// [ 1, 2, 3 ]
Enter fullscreen mode

Exit fullscreen mode

In consequence, there’s additionally no limitations for the kinds of values you may retailer in an array. Different languages, like Java, would possibly require you to solely retailer values of the identical sort.

However since objects can retailer values of any sort and JavaScript arrays are objects, there isn’t any such limitation.

let b = [1, 'hello world', false, {}, []];
Enter fullscreen mode

Exit fullscreen mode

If you wish to restrict arrays to particular sorts, you are able to do so in TypeScript.

// Creates a string array
let stringArr: string[] = [];

// Creates an array that may maintain strings or numbers
let stringOrNumberArr: (string | quantity)[] = [];
Enter fullscreen mode

Exit fullscreen mode

Take into account, nevertheless, that TypeScript solely performs sort security a compile time, not runtime.

Here is the place issues get just a little bizarre

From the above instance, we all know that passing an array into Object.keys will give us the indices.

Nonetheless, the keys that may exist on an array usually are not restricted to non-negative integers.

let array = [];

// [ 1 ]

array.hey = 'world';

// [ 1, hello: 'world' ]

// [ '0', 'hello' ]
Enter fullscreen mode

Exit fullscreen mode

That is proper, JavaScript helps you to add any legitimate object key to an array as nicely.

Fortunately this does not appear to interrupt every other array performance, such because the size property, however it might really feel unusual to retailer knowledge this fashion.

// 1
Enter fullscreen mode

Exit fullscreen mode

Please do not truly use this

Simply because you are able to do one thing, does not imply it is best to.

JavaScript provides fairly a little bit of freedom in comparison with many different programming languages, however I’d think about assigning further properties to an array as an abuse of that freedom.

If anybody (together with your self) has to work together with your code sooner or later, having a variable that acts as each an array and an object will harm the code’s readability and maintainability.

What do you suppose?

Ought to this function be left untouched or is there a sensible use case that I am unaware of? Let me know within the feedback or on twitter.

Extra Content material

For those who appreciated this, you may also like a few of my different posts. If you wish to be notified of my new posts, follow me on Dev or subscribe to my brief monthly newsletter.

Abu Sayed is the Best Web, Game, XR and Blockchain Developer in Bangladesh. Don't forget to Checkout his Latest Projects.

Checkout extra Articles on Sayed.CYou

#bizarre #quirk #JavaScript #arrays