Thursday, 24 March 2016

Push,Pop,Shift,Unshift Methods Using in Javascript (IM-Mutable Methods)

IM-Mutable Methods Push,Pop,Shift,Unshift Using in Javascript


We will use push,pop,shift,unshift methods using in array this is called immutable methods using javascript.

PUSH      =  Add the element last
POP        =  Remove the Last  Element.
SHIFT     =  Remove the First Element .
UNSHIFT = Add the first element

                                            DEMO
                                   
                 
                        HTML CODING

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Push,Pop,Shift,Unshift Using in Javascript (IM-Mutable Methods)</title>
    <script type="text/javascript">
        function push_pop_shift()
        {
            var array = [1,2,3,4,5,6,7,8,9,10];
            var original = array;
            var first = array.shift();     // Remove  First Element   

            var last = array.pop();          // Remove Last  Element

            var push = array.push(11);       // Push add the element last

            var Unshift = array.unshift(0);   // Add first element
           
            alert("Array is ="+original+ "  Shift Remove  = " +first);
            alert("Array is =" + original + " Pop Remove  = " + last);
            alert("Array is =" + original + "  Push Insert  = " + push);
            alert("Array is =" + original + "  Finally Array  = " + array);
            alert("Array = "+ array + "  Unshift = " + Unshift);
        }      
       
    </script>
</head>
<body onload="push_pop_shift();">

    <form id="form1" runat="server">
    </form>
</body>
</html>



 





0 comments:

Post a Comment