Hello Everyone.
Firstly we should know What is Array.
As we know we have different Array methods are there.
Array: An array is a special variable, which can hold more than one value
const array_name = [item1, item2, ...];
const cars = ["Saab","Volvo","BMW"];
Note: Array indexes start with 0.
[0] is the first element. [1] is the second element.
Converting Arrays to Strings:
const fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.toString(); //Banana,Orange,Apple,Mango
Popping and Pushing:
pop()
method removes the last element from an array
const fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.push("Kiwi"); //Banana,Orange,Apple,Mango,Kiwi
push()
method returns the new array length
const fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.pop("Kiwi"); //Mango
Array shift() & unShift():
The shift()
method removes the first element from an array and returns that removed element
const array1 = [1, 2, 3];
console.log(array1.shift());// 1
UnShift():
The unshift()
method adds a new element to an array (at the beginning), and "unshifts" older elements:
const array1 = [1, 2, 3];
console.log(array1.unshift(4, 5));
// Expected output: 5
console.log(array1);
// Expected output: Array [4, 5, 1, 2, 3]
Splice() And Slice():
splice method will return the modified array.Splice() method will take 3 arguments.
Syntax: array.splice(index, howMany, item1, item2, ...)
let array = [1, 2, 3, 4, 5];
let removed = array. splice(1, 2);
console.log(array); // [1, 4, 5]
console.log(removed); // [2, 3]
Slice():
The unshift method returns the new array.
const fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"];
const citrus = fruits.slice(1, 3);
sort():
The sort()
method sorts an array alphabetically
const months = ['March', 'Jan', 'Feb', 'Dec'];
months.sort();
console.log(months);
// Expected output: Array ["Dec", "Feb", "Jan", "March"]
reverse():
The reverse()
method reverses the elements in an array
const items = [1, 2, 3];
console.log(items); // [1, 2, 3]
items.reverse();
console.log(items);// [3, 2, 1]