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
var array_name = [item1, item2, …];
var cars = [“Suzuki”, “Cultus”, “BMW”];
A statement may span multiple lines. The spaces and line breaks are not important.
var cars = [
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 = “dog”;
2 pets = “cat”;
3 pets = “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 = “lizard”;
2 pets = “snake”;
If we mention to pets, we’ll get “lizard”. If we refer to pets, we’ll get “snake”. But if we refer to pets through pets or pets or pets, 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 = “lizard”;
2 pets = “fish”;
3 pets = “gerbil”;
4 pets = “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.
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.
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.
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.
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
let arr = [‘This is the first element’, ‘This is the second element’, and ‘This is the last element’]
console.log(arr) // logs ‘this is the first element’
console.log(arr) // 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
Use of Array and Objects
- 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 ( )
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