In JavaScript, Array
is a built-in global object that allows you to store multiple elements at once.
What is Array in JS?
The Array object, as with arrays in other programming languages, enables storing a collection of multiple items under a single variable name.
Declaring arrays
We can declare arrays in two different ways.
Using new Array
With a new Array, we could specify the elements we want to exist within the array, like this:
const students = new Array('Shubham', 'Ravikant');
console.log(students.length);
Array literal notation
Declaring with an array literal, we specify the values that the array will have. If we don’t declare any values, the array will be empty.
// 'students' array created using array literal notation.
const fruits = ['Shubham', 'Ravikant'];
console.log(students.length);
Javascript Array Methods
Here is a list of the methods of the Array object along with their examples.
.reverse()
It Returns the array in reverse order.
let numberofstudents = [10, 20, 30, 40, 50];
// reversing the numbers array
let reversedArray = numberofstudents.reverse();
console.log(reversedArray);
// Output: [ 50, 40, 30, 20, 10 ]
.sort()
The sort()
method sorts the items of an array in a specific order (ascending or descending).
let developedcities = ["California", "Barcelona", "Paris", "Kathmandu"];
// sort the city array in ascending order
let sortedArray = developedcities.sort();
console.log(sortedArray);
// Output: [ 'Barcelona', 'California', 'Kathmandu', 'Paris' ]
.fill()
The fill()
method returns an array by filling all elements with a specified value.
// defining an array
var fruits = ['Apple', 'Banana', 'Grape'];
// filling every element of the array with 'Cherry'
fruits.fill("Cherry");
console.log(fruits);
// Output:
// [ 'Cherry', 'Cherry', 'Cherry' ]
.join()
The join()
method returns a new string by concatenating all of the elements in an array, separated by a specified separator.
let message = ["We", "are", "coders."];
// join all elements of array using space
let joinedMessage = message.join(" ");
console.log(joinedMessage);
// Output: We are coders.
.push()
The push()
method adds zero or more elements to the end of the array.
let city = ["New York", "Madrid", "Kathmandu"];
// add "London" to the array
city.push("India");
console.log(city);
// Output: [ 'New York', 'Madrid', 'Kathmandu', 'India' ]
.pop()
The pop()
method removes the last element from an array and returns that element.
let cities = ["Madrid", "New York", "Kathmandu", "Paris"];
// remove the last element
let removedCity = cities.pop();
console.log(cities) // ["Madrid", "New York", "Kathmandu"]
console.log(removedCity); // Paris
.shift()
The shift()
method removes the first element from an array and returns that element.
let languages = ["English", "Java", "Python", "JavaScript"];
// removes the first element of the array
let first = languages.shift();
console.log(first);
console.log(languages);
// Output: English
// [ 'Java', 'Python', 'JavaScript' ]
.unshift()
The unshift()
method adds one or more elements to the beginning of an array and returns the new length of the array.
let languages = ["Java", "Python", "C"];
// add "JavaScript" at the beginning of the array
languages.unshift("JavaScript");
console.log(languages);
// Output: [ 'JavaScript', 'Java', 'Python', 'C' ]
.concat()
The concat()
method returns a new array by merging two or more values/arrays.
let primeNumbers = [2, 3, 5, 7]
let evenNumbers = [2, 4, 6, 8]
// join two arrays
let joinedArrays = primeNumbers.concat(evenNumbers);
console.log(joinedArrays);
/* Output:
[
2, 3, 5, 7,
2, 4, 6, 8
]
*/
.splice()
The splice()
method returns an array by changing (adding/removing) its elements in place.
let prime_numbers = [2, 3, 5, 7, 9, 11];
// replace 1 element from index 4 by 13
let removedElement = prime_numbers.splice(4, 1, 13);
console.log(removedElement);
console.log(prime_numbers);
// Output: [ 9 ]
// [ 2, 3, 5, 7, 13, 11 ]
.IndexOf()
The indexOf()
method returns the first index of occurance of an array element, or -1 if it is not found.
let languages = ["Java", "JavaScript", "Python", "JavaScript"];
// get the index of the first occurrence of "JavaScript"
let index = languages.indexOf("JavaScript");
console.log(index);
// Output: 1
.lastIndexOf()
The lastIndexOf()
method returns the index of the last occurrence of a specified element in the array.
let priceList = [10, 8, 2, 31, 10, 31, 65];
// finding the index of the last occurence of 31
let lastIndex = priceList.lastIndexOf(31);
console.log(lastIndex);
// Output: 5
.findIndex()
The findIndex()
method returns the index of the first array element that satisfies the provided test function or else returns -1.
// function that returns odd number
function isOdd(element) {
return element % 2 !== 0;
}
// defining an array of integers
let numbers = [2, 8, 1, 3, 4];
// returns the index of the first odd number in the array
let firstOdd = numbers.findIndex(isOdd);
console.log(firstOdd);
// Output: 2
.includes()
The includes()
method checks if an array contains a specified element or not.
// defining an array
let languages = ["JavaScript", "Java", "C"];
// checking whether the array contains 'Java'
let check = languages.includes("Java");
console.log(check);
// Output: true
.isArray()
The isArray()
method checks whether the passed argument is an array or not.
let numbers = [1, 2, 3, 4];
// checking whether numbers is an array or not
console.log(Array.isArray(numbers));
let text = "JavaScript";
// checking whether text is an array or not
console.log(Array.isArray(text));
// Output:
// true
// false
.map()
The map()
method creates a new array with the results of calling a function for every array element.
let numbers = [2, 4, 6, 8, 10];
// function to return the square of a number
function square(number) {
return number * number;
}
// apply square() function to each item of the numbers list
let square_numbers = numbers.map(square);
console.log(square_numbers);
// Output: [ 4, 16, 36, 64, 100 ]
.flatMap()
The flatMap()
method first maps each element of an array using a mapping function, then flattens it into a new array.
// defining an array
let numbers = [1, 2, 3, 4, 5];
// each element of the array is squared and later flattened
const resultingArray = numbers.flatMap((x) => [x ** 2]);
console.log(resultingArray);
// Output:
// [ 1, 4, 9, 16, 25 ]
THESE ALL ARE THE JAVASCRIPT METHODS...........................