How to use JavaScript Array unshift()
Array unshift
On this page
Array unshift() method
Program to use JavaScript Array unshift() method
let a = [8,10,12,14,16,18,20];
let b = a.unshift(2,4,6);
console.log(a);
console.log(b);
Output
[
2, 4, 6, 8, 10,
12, 14, 16, 18, 20
]
10
Steps Description
- Declare two variable a and b
- Initialize two variable with value of array a = [8,10,12,14,16,18,20] and b = a.unshift(2,4,6)
- Array unshift() method appends one or more elements to the beginning of an array and returns the length of the new array.
- The value of b variable is displayed with the help of console.log inbuilt function
Array unshift() method
Program to use JavaScript Array unshift() method
Program to use JavaScript Array unshift() method
let c = [ 'Cat', 'Dog', 'Tiger', 'Lion'];
let d = c.unshift('Elephant', 'Bear');
console.log(c);
console.log(d);
Output
[ 'Elephant', 'Bear', 'Cat', 'Dog', 'Tiger', 'Lion' ]
Steps Description
- Declare two variable c and d
- Initialize two variable with value of array c = [ ‘Cat’, ‘Dog’, ‘Tiger’, ‘Lion’] and d = c.unshift(‘Elephant’, ‘Bear’)
- Array unshift() method appends one or more elements to the beginning of an array and returns the length of the new array.
- The value of d variable is displayed with the help of console.log inbuilt function