What is an array in Javascript?
In JavaScript, arrays can be a collection of elements of any type. This means that you can create an array with elements of type String, Boolean, Number, Objects, and even other Arrays.
A pair of square brackets []
represents an array in JavaScript. All the elements in the array are comma(,)
separated.
Here is an example of an array with four elements: type Number, Boolean, String, and Object.
const mixedTypedArray = [50, true, "Javascript", {}];
JavaScript Array Methods:-
push()
– Insert an element at the end of the array.
Example:-
const names = ["Henry","John","Mike"];
names.push("Shaun");
unshift()
– Insert an element at the beginning of the array.
Example:-
const names = ["Henry","John","Mike"];
names.unshift("Bella","David");
pop()
– Remove an element from the end of the array.
Example:-
const names = ["Henry","John","Mike","David"];
names.pop();
shift()
– Remove an element from the beginning of the array.
Example:-
const names = ["Henry","John","Mike","David"];
names.shift();
slice()
– Create a shallow copy of an array.
Example:-
const names = ["Henry","John","Mike","David"];
const new = names.slice(2);
length
– Determine the size of an array.
Example:-
const names = ["Henry","John","Mike","David"];
let length = names.length;
Array.isArray()
– Determine if a value is an array.
Example:-
const names = ["Henry","John","Mike","David"];
let result = Array.isArray(names);