A word about arrays in "Modern" Javascript

Septiembre 24, 2021

Here are three things to consider when using arrays in “Modern” Javascript.

Defining with const

In Javascript, when you assign a variable of type array to another variable, what you are doing is assigning a reference.

let a=[1,2,3];
let b=a; //Now b also refers (is an alias if you will) to b. b is not a copy of a.

By defining arrays with const, we are preventing changes to the reference.

const a=[1,2,3];
const fruits=[“apple”, “orange”, “banana”];
a=fruits; //Error

Iterating with for

There are three ways: Standard, OK, and modern but unoptimized.

What’s happening is modern Javascript introduces ways of iterating through Objects by using for loops, and these methods can also be used to iterate through Arrays since everything is an object in Javascript.

However, the modern additions are meant for Objects and not Arrays.

Visit https://javascript.info/array to learn more about arrays.