0 votes
in JavaScript by

What will be the output obtained by "shift ()" in the given code of JavaScript?

var a =[];  

a.unshift(5);  

a.unshift(22);  

a.shift();  

a.unshift(3,[4,5]);  

a.shift();  

a.shift();  

a.shift();  



a) Exception is thrown

b) [4,5]

c) [3,4,5]

d) 5

1 Answer

0 votes
by

Answer: D

Reason: In JavaScript, the "unshift()"," shift()" methods work like just as push() and pop() but with a slight change, unlike the push and pop the unshift(), unshift() both insert and remove a data item from the beginning instead of from the end of the array. The "unshift()" is used for inserting the data element/item in the beginning of the array while the "shift()" method shifts the data item to the beginning of the array from the higher index, empty the last index of the array and returns the updated length of the array.

In the process of shifting, the data element is removed from the beginning of the array, and all subsequent elements are shifted and the new length of the array is returned.

Related questions

0 votes
asked Mar 23, 2021 in JavaScript by sharadyadav1986
+1 vote
asked Oct 9, 2022 in JavaScript by Robin
...