π― With push()
The push() function is used to add data to the end of an array.
It can take one or more parameters as shown with an example:
{
var arr1 = [1,2,3];
arr1.push(4);
var arr2 = [βStimpsonβ, βjβ ,βcatβ];
arr1.push([βhappyβ , βjoyβ]);
}
π― With pop()
The pop() function is the opposite of push() , which removes data from the end of an array.
Any type of data can be popped off from the array with an example:
{
var threeArr = [1,4,6];
var oneDown = threeArr.pop();
console.log(oneDown);
console.log(threeArr);
}
π― With unshift()
The unshift() function can unshift elements from either the end or the beginning of the array
Example:
{
var arr = [βStimpsonβ, βjβ ,βcatβ];
arr.shift();
arr.unshift(βhappyβ);
after shift, arr will have value [βjβ ,βcatβ], after unshift() the arr would have [βhappyβ, βjβ ,βcatβ]
}
π― With shift()
The shift() function can shift elements from either the end or the beginning of the array
Example:
{
var arr = [βStimpsonβ, βjβ ,βcatβ];
var removedFromarr = arr.shift();
after shift, arr will have value [βjβ ,βcatβ], after shift() the arr would have [βhappyβ, βjβ ,βcatβ]
}
Top comments (0)