JavaScript Arrays Are Weirdly Named

Everyone else calls them lists

Ikechi Michael
3 min readJul 6, 2019

Yesterday on Twitter, I was tagged by Abdulazeez Abdulazeez Adeshina in a conversation and the topic was why arrays in JavaScript are dynamic (non-typed).

Image result for arrays
I hope you’ll see the Irony after this article. Image Source

Note: The person who asked the question was very new to programming and didn’t know what it meant for a language to be strongly-typed or otherwise, but I didn’t have this context when I began replying, or my answer might have been very different.

I was tempted to just reply “Well, JavaScript is a dynamic language, where you don’t really need to define types for variables,” but I thought a deeper, more insightful answer would help, so I began to think about it.

Defining JavaScript Arrays

It began with the definition of arrays I learned, “A contiguous set of memory blocks, accessed by a common reference.”

Tutorials Point gives it as, “An array is a data structure which can store a fixed-size collection of elements of the same data type.”

This definition gives out JavaScript arrays as special because they don’t require:

A fixed size

JavaScript arrays can be empty one moment and contain 500 elements in the next.

The same data-type for its elements

A JavaScript array can contain a string, number, date, object, and another array at the same time.

const arr = ["Hello", 123, new Date(), { message: "Yo!" }, [1,2,3]]

At this point, you might be saying, “Michael, things in JavaScript are usually weird. What’s your point?”

The point is, what is popularly called an array in JavaScript, really isn’t.

So, What Is It, and Why Is It So?

To answer these questions, we’ll look at why arrays are the way they are, and for this, we’ll go back all the way to C, the grandparent of most programming languages.

In C, one way to declare an array is with malloc, a function that means “allocate memory”. With malloc, you actually have to consider the size in bytes, of each element in the array.

malloc(5 * sizeof(int));

Note: int means “integer”, a number without a decimal point that is between -2³² and +2³².

In the expression above, we’re saying “allocate a block of memory, that is five times, the size of an int.

Now, the size of an integer is two bytes on a 16-bit computer, so with this type of computer, that expression would allocate 5 * 2 bytes, or 10 bytes of memory, as an array.

Now, you know that int has a size in bytes. What about other data types?

char => 1 byte
long => 8 bytes
float => 4 bytes
double => 8 bytes

Now, you know why it is important to know:

  • The data type of elements when creating an array
  • How many elements the array should contain

If it’s still not clear, it’s because the program needs to know how many bytes of memory to allocate.

But a dynamic language like JavaScript does not have types, so it wouldn’t know the data type of array elements. How does it know how much bytes to allocate?

Exactly!

It doesn’t, because what it calls arrays, aren’t.

How do other dynamic languages manage this?

It’s impossible to manage actual arrays without types, so a dynamic language like Python does not even bother. The w3schools page for Python arrays says:

Note: Python does not have built-in support for Arrays, but Python Lists can be used instead.

Python just calls them lists, to prevent confusion.

Why, JavaScript? Why?

So, why exactly did JavaScript stick to calling them arrays?

I guess it would be to ease the transition for people coming from other backgrounds where “arrays” are the nomenclature for working with a collection of objects.

To be fair, JavaScript isn’t alone in this. I see you too, PHP.

Real arrays in JavaScript

In the same spirit of fairness, JavaScript does have actual arrays that behave as an Array should, but they have statically typed elements.

See:

--

--

Ikechi Michael
Ikechi Michael

Written by Ikechi Michael

I’ve learned I don’t know anything. I've also learned that people will pay for what I know. Maybe that's why they never pay.

Responses (1)