Engine Failure: Array is gone - can we recover?

submited by
Style Pass
2024-02-11 23:30:07

Due to a severe engine failure, our JavaScript engine has lost Arrays. They are gone. With the help of historical usage of Arrays, we will try to recover from this. Is it possible? Or will future generations need to live without Arrays?

Unfortunately, we can’t restore syntax, so creating arrays via bracket notation (i.e. a = []) won’t work. Let’s try this instead:

Phew, that works. But why? A JavaScript object is a collection of key-value pairs (AKA map or dictionary) with arbitrary values; keys are always strings, though1. Everything that you put into the square brackets will be converted to a string automatically:

There’s also a shorthand for accessing properties. If the property name is a valid identifier2, you can use dot notation for reading or writing values:

That works. But can we calculate it instead? Whenever we add something to our list, we need to update it. And who has time for that? What if we, instead, calculate the length? We can iterate over the keys of an Object:

Leave a Comment