What Are JavaScript Arrays?

 What Are JavaScript Arrays? 

What Are JavaScript Arrays?

Description

The Arrays are list-like objects. Their example has methods to perform traversal and alteration operations. JavaScript array length and types of its elements are fixed. Meanwhile an array’s length may change at any time. The data may be stored at non-contiguous locations in the array. JavaScript arrays are not assured to be dense. This depends on the choice of programmer that how he chooses to use them. These are suitable features if are not desirable for our specific use we may consider using typed arrays.

Arrays may not use strings as element indexes. They must use integers. Bracket notation or dot notation are being used for setting or accessing through non-integers. They will not set or recover an element from the array list itself. The variable can be set or accessed related with that array’s object property group. The array’s traversal and mutation operation may not be applied to the separation of object properties and list of array elements.

Creating an Array

For creating a JavaScript Array by using an array literal is the easiest way.

Syntax:

var array_name = [item1, item2, …];      

Example

var cars = [“Suzuki”, “Cultus”, “BMW”];

A statement may span multiple lines. The spaces and line breaks are not important.

Example

var cars = [

“Suzuki”,

   “Cultus”,

   “BMW”

];

Adding and removing elements in Array

We can declare an empty variable, one that doesn’t have a value. Then we can assign it a value whenever we like. And we can change its value at will. We may do all these things with an array, as well. This is how we declare an empty array.

var pets = [];

We assign values to the array pets

1 pets[0] = “dog”;

2 pets[1] = “cat”;

3 pets[2] = “bird”;

First three elements of this array in this example are in order. We can legally leave breaks in an array if we choose to. We not normally would. Suppose that we start with the same empty array and code these lines.

1 pets[3] = “lizard”;

2 pets[6] = “snake”;

If we mention to pets[3], we’ll get “lizard”. If we refer to pets[6], we’ll get “snake”. But if we refer to pets[0] through pets[2] or pets[4] or pets[5], we’ll get undefined. We may assign additional values to an array that already has values. Assume that the first

Three (03) elements of the array pets are “dog”, “cat”, and “bird”. Formerly we write this code.

1 pets[3] = “lizard”;

2 pets[4] = “fish”;

3 pets[5] = “gerbil”;

4 pets[6] = “snake”;

Currently the array has 7 elements: “dog”, “cat”, “bird”, “lizard”, “fish”, “gerbil”, and “snake”. The old value is replaced by the new one if we allocate a new value to an array element that already has one. We can remove the last element of an array by using the keyword, pop. Assume we have an array, pets, whose elements are “dog”, “cat”, and “bird”. The below code deletes the last element, “bird”, leaving a two-element array.

pets.pop();

We can add one or more elements to the end of an array by using the keyword, push.

Believe we have that same array consisting of “dog”, “cat”, and “bird”. The under mentioned code adds two new elements to the end of the array.

pets.push(“fish”, “ferret”);

We use the shift method to remove an element from the start of an array. Suppose we have an array, pets, whose elements are “dog”, “cat”, and “bird”. The following removes the first element, “dog”, leaving us with a two-element array.

pets.shift();

We use the unshift method to add one or more elements to the start of an array. The below mentioned code adds two elements to the start of the array.

pets.unshift(“fish”, “ferret”);

We use the splice method to insert one or more elements anywhere in an array. This is optionally removing one or more elements that come after it. Suppose we have an array with the elements “dog”, “cat”, “fly”, “bug”, “ox”. The below described code adds “pig”, “duck”, and “emu” after “cat” while removing “fly” and “bug”.

pets.splice (2, 2, “pig”, “duck”, “emu”);

Accessing array elements

The JavaScript arrays are zero-indexed. The first element of an array is at index 0.The final element is at the index value equal to the value of the array’s length property minus 1. We may access an array element by referring to the index number. Using an invalid index number returns undefined.

let arr = [‘This is the first element’, ‘This is the second element’, and ‘This is the last element’]

console.log(arr[0])                              // logs ‘this is the first element’

console.log(arr[1])                               // logs ‘this is the second element’

At the last, console.log(arr [arr.length – 1])            // logs ‘this is the last element’

Array elements are object properties in the same way that to String is a property (to be specific, however, to String () is a method). Yet, trying to access an element of an array as follows throws a syntax error because the property name is not valid:

Difference between Arrays and Objects

The arrays use numbered indexes which are special kind of objects and objects use named indexes in JavaScript.

Use of Array and Objects

  • Associative arrays are not being supported by the JavaScript.
  • We should use objects when we want the element names to be strings (text).
  • We should use arrays when we want the element names to be numbers.

Avoid new Array ( )

Using the JavaScript’s built-in array constructor new Array ( ) is not necessary.

Use [ ] instead.

Following two different declarations create a new empty array named points:

var points = new Array();     // Bad

var points = [];             // Good

Following two dissimilar statements create a new array containing 6 numbers:

var points = new Array(40, 100, 1, 5, 25, 10);             // Bad and var points = [40, 100, 1, 5, 25, 10];                              // Good

 

Leave a Comment