JavaScript Arrays
Arrays in JavaScript are a primitive data type to store values that are more than one within one variable.
Example:
let cars
=
["Thar", "Scorpio", "BMW"];
Array Methods
JavaScript provides a lot of methods for array manipulation.
length
The length method returns the size of an array.
Example:
let myArr
=
["Thar", "BMW", "Audi"];
console.log(myArr.length);
Output:
3
push
The push() method is used to insert an element to the end of an array.
Example:
let myArr
=
["Thar", "Audi", "BMW"];
myArr.push("Scorpio");
console.log(myArr);
Output:
[ 'Thar', 'Audi', 'BMW', 'Scorpio' ]
pop
The pop() function is employed to delete the last element of an array.
Example:
let myArr
=
["Thar", "Audi", "BMW"];
myArr.pop();
console.log(myArr);
Output:
[ 'Thar', 'Audi' ]