June 7, 2023
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
andb
- Initialize two variable with value of array
a = [8,10,12,14,16,18,20]
andb = 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
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' ]
6
Steps Description
- Declare two variable
c
andd
- Initialize two variable with value of array
c = [ 'Cat', 'Dog', 'Tiger', 'Lion']
andd = 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
by : Suhel Akhtar
Quick Summary:
JavaScript Array unshift() method