Table of contents
Array -
An array in JavaScript is an object data type, it can store similler annd different kind data types.
For example - it can store string, numbers, object and even other arrays, And also we can store mix of it.
const element = ["Car", "Bus", "Bike", 1, 45, 76 [2,4,6,8]];
An array contains more than one value with a numerical index, where the index starts from 0. Thus it holds its value in a key-value pair.
There are 2 ways to construct array in JavaScript -
- By array literal
By using new keword -
- Instance of Array (using new keyword)
- Array constructor (using new keyword)
1. JavaScript array literal-
A collection of elements wrapped between the pair of square brackets [ ] represent an array literal in JavaScript. The size of this array literal is specified by the element inside the brackets, and values are separated by comma ( , ).
The syntax of creating array using array literal is given below:
var arrayName = [value2, value2, value3, ....., valueN];
Let's see the simple example of creating and using array in JavaScript.
var arr = ["Sam", "Jerry", "Rock"];
for (i = 0; i < arr.length; i++){
console.log(arr[i]);
}
2. Creating array using 'new' keyword -
New keyword in JavaScript is used to create an instance of an object that has a constructor function. On calling the constructor function with ‘new’ operator, the following actions are taken:
- A new empty object is created.
- The new object’s internal ‘Prototype’ property (proto) is set the same as the prototype of the constructing function.
- The ‘this’ variable is made to point to the newly created object. It binds the property which is declared with ‘this’ keyword to the new object.
- A newly created object is returned when the constructor function returns a non-primitive value (custom JavaScript object). If the constructor function returns a primitive value, it will be ignored. At the end of the function, ‘this’ is returned if there is no return statement in the function body.
The syntax of creating array directly using new-
var arrayName = new Array ();
Creating instance of array using new keywor -
var i;
var emp = new Array ();
emp [0] = "Harry";
emp [1] = "Duke";
emp [2] = "Jackson";
for ( i = 0; i < emp.length; i++){
console.log(emp[i]);
}
Creating array constructor using new keywor -
var emp = new Array("Rohan","Henrry","Smith"); for ( i = 0; i < emp.length; i++){ console.log(emp[i]); }
Basic operations on arrays
- Adding an element to the end of an array-
let seaName = ['Black Sea', 'Caribbean Sea', 'North Sea', 'Baltic Sea'];
seaName.push('Red Sea');
console.log(seaName);
Output -
[ 'Black Sea', 'Caribbean Sea', 'North Sea', 'Baltic Sea', 'Red Sea' ]
- Adding an element to the beginning of an array-
let seaName = ['Black Sea', 'Caribbean Sea', 'North Sea', 'Baltic Sea'];
seaName.unshift('Red Sea');
console.log(seaName);
Output -
[ 'Red Sea', 'Black Sea', 'Caribbean Sea', 'North Sea', 'Baltic Sea' ]
- Removing an element from the end of an array-
Output -let seaName = ['Black Sea', 'Caribbean Sea', 'North Sea', 'Baltic Sea']; const lastElement = seaName.pop(); console.log(lastElement);
Baltic Sea
- Finding an index of an element in the array-
let country = ['India', 'Japan', 'South Korea', 'USA'];
let index = country.indexOf('USA');
console.log(index); //4
Some Methods used in array operation are below -
Methods | Description |
concat ( ) | It returns a new array object that contains two or more merged arrays. |
copywithin ( ) | It copies the part of the given array with its own elements and returns the modified array. |
every ( ) | It determines whether all the elements of an array are satisfying the provided function conditions. |
find ( ) | It returns the value of the first element in the given array that satisfies the specified condition. |
findIndex ( ) | It returns the index value of the first element in the given array that satisfies the specified condition. |
forEach ( ) | It invokes the provided function once for each element of an array. |
indexOf ( ) | It searches the specified element in the given array and returns the index of the first match. |
map ( ) | It calls the specified function for every array element and returns the new array |
lastIndexOf ( ) | It searches the specified element in the given array and returns the index of the last match. |
pop ( ) | It removes and returns the last element of an array. |
push ( ) | It adds one or more elements to the end of an array. |
reverse ( ) | It reverses the elements of given array. |
shift ( ) | It removes and returns the first element of an array. |
unshift ( ) | It adds one or more elements in the beginning of the given array. |
slice ( ) | It returns a new array containing the copy of the part of the given array. |
splice ( ) | It add/remove elements to/from the given array. |
toString ( ) | It converts the elements of a specified array into string form, without affecting the original array. |
sort ( ) | It returns the element of the given array in a sorted order. |