What is unshift in Javascript | Demo| Example

Here We can learn Unshift() method very easy with Example . Below we have given example and Demo you can check it let me know if you are getting any issue with unshift() method  The arrA.unshift() and arrB.unshift()  method is used to add one or more elements to the beginning of the given array. This function increases the length of the existing array by the number of elements added to the array.

Demo URL



Syntax: 

arrayUnshift.unshift(el1, el2, el2, ..., eX)

Parameters: This method accept a single parameters.


  • el: This parameter elements to be added at the beginning of the array.

Return value: This function returns the new length of the array after inserting the arguments at the beginning of the array.

Below examples illustrate the JavaScript ArrayUnshift unshift() method:

  • Example 1: In this example the function unshift() adds 28 and 65 to the front of the array.
    // Example 1
    var arrA = [5,26,39,45,90];
    console.log(arrA.unshift(27,39));
    console.log(arrA);
    

    Output:

  • Example 2: In this example the shift() method tries to remove the first element of the array, but the array is empty, therefore it returns undefined.
    // Example 2 
    var arrB = [20,45,75,80,90,100];
    console.log(arrB.unshift());
    console.log(arrB);
    

    Output:

No comments:

Post a Comment

CPU vs GPU Architecture

  CPU vs GPU Architecture CPU (Central Processing Unit) and GPU (Graphics Processing Unit) have distinct architectural differences, optimize...

Best for you