Learn: Array

Simply Array is to store multiple values in one variable.

Syntax:

const array_name = [item1, item2, ...]; ( Here we assign array literal to the variable array_name)

Why Arrays?

If we want to store 3 values we can store in three variables like

let car1 = "Saab";
let car2 = "Volvo";
let car3 = "BMW";

But what if we have to store the 100 values it is a bit more difficult right? So here came the array by using an array we can store the many values in one variable like

const cars = ["Saab", "Volvo", "BMW"];

In an array, the line breaks and spaces are not considered.

Creating Array

You can also create an empty array and provide values to it by using indexing.

const cars = [];
cars[0]= "Saab";
cars[1]= "Volvo";
cars[2]= "BMW";

We can also create the array by using the array keyword new.

const cars = new Array("Saab", "Volvo", "BMW");

Note: The two examples above do the same.

There is no need to use new Array().

For simplicity, readability and execution speed, use the array literal method.

Accessing Array Elements

You can access an array element by referring to the index number. Array index starts with 0 and so on...

const cars = ["Saab", "Volvo", "BMW"];
let car = cars[0];

Changing an Array Element

cars[0] = "Opel"; It replaces the 0th index with the value Opel.

Accessing the Full Array

In JavaScript, the full array can be accessed by referring to the array name.

console.log(cars);

Arrays are Objects

Arrays are a special type of object. The typeof() method in JavaScript returns "object" for arrays.

But, JavaScript arrays are best described as arrays.

Arrays use numbers person[0] to access their "elements".

const person = ["John", "Doe", 46];

Objects use names person.firstName to access their "members".

const person = {firstName: "John", lastName: "Doe", age:46};

Array Elements Can Be Objects

JavaScript variables can be objects. Arrays are special kinds of objects.

Because of this, you can have variables of different types in the same Array.

You can have objects in an Array. You can have functions in an Array. You can have arrays in an Array.

myArray[0] = Date.now;
myArray[1] = myFunction;
myArray[2] = myCars;

Length

The length property of an array returns the length of an array (the number of array elements).

Accessing the Last Array Element

const fruits = ["Banana", "Orange", "Apple", "Mango"];
let fruit = fruits[fruits.length - 1];

Looping Array Elements

//Using for loop
const fruits = ["Banana", "Orange", "Apple", "Mango"];
let fLen = fruits.length;

let text = "<ul>";
for (let i = 0; i < fLen; i++) {
  text += "<li>" + fruits[i] + "</li>";
}
text += "</ul>";

//using forEach
const fruits = ["Banana", "Orange", "Apple", "Mango"];

let text = "<ul>";
fruits.forEach(myFunction);
text += "</ul>";

function myFunction(value) {
  text += "<li>" + value + "</li>";
}

To add a new element to an array is to use the array.push(value) method.

A new element can also be added to an array using the Array[fruits.length]=value property.

Array And Object Diff

In JavaScript, arrays use numbered indexes.

In JavaScript, objects use named indexes.

Note:

Arrays are a special kind of objects, with numbered indexes.

JavaScript does not support arrays with named indexes(associative arrays).

In JavaScript, arrays always use numbered indexes.

const person = []; person[0] = "John"; person[1] = "Doe"; person[2] = 46; person.length; // Will return 3 person[0]; //"John"

WARNING :
If you use named indexes, JavaScript will redefine the array to an object.

After that, some array methods and properties will produce incorrect results.

const person = []; person["firstName"] = "John"; person["lastName"] = "Doe"; person["age"] = 46; person.length; // Will return 0 person[0]; // Will return undefined

When to Use Arrays and when to use Objects.

  • JavaScript does not support associative arrays.

  • You should use objects when you want the element names to be strings (text).

  • You should use arrays when you want the element names to be numbers.

0
Subscribe to my newsletter

Read articles from Naresh Chandragiri directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Naresh Chandragiri
Naresh Chandragiri

I am a currently undergraduate student from computer science background. I'm currently learning full stack web development.