Arrays in Javascript
Arrays in Javascript are a variable that allows storing a collection of multiple items. Ex:- const fruits=["apple","banana","kiwi"];
Accesing arrays
const fruits=["apple","banana","kiwi"];
let fruit=fruits[0]
console.log(fruit)
//apple
Changing an array element
const fruits=["apple","banana","kiwi"];
fruits[0]="grapes";
console.log(fruits);
The length property
const fruits=["apple","banana","kiwi"];
console.log(fruits.length);
//3
Adding array elements using push()
const fruits=["apple","banana","kiwi"];
fruits.push("orange");
console.log(fruits);
//apple,banana,kiwi,orange
Removing array elements using pop()
const fruits=["apple","banana","kiwi","orange"];
fruits.pop();
console.log(fruits);
//apple,banana,kiwi
shift() method
It removes the first element of an array and shifts other elements to the left.
const fruits=["apple","banana","kiwi,"orange"];
fruits.shift();
console.log(fruits);
//"banana","kiwi","orange"
unshift() method
The unshift method adds new elements to the beginning of an array.
const fruits=["apple","banana","kiwi,"orange"];
fruits.unshift("mango");
console.log(fruits);
//"mango","apple","banana","kiwi","orange"
concat() method:-
The concat method is used to concatenate or merge two or more arrays.
const arr1=["apple","banana","kiwi"];
const arr2=["Honda","Toyota"];
console.log(arr1.concat(arr2));
//"apple","banana","kiwi","Honda","Toyota".