Page 242 - Web_Application_v2.0_C12_Fb
P. 242

<SCRIPT>
                  const Fruits = ['Apple', 'Mango', 'Banana', 'Papaya'];

                  document.getElementById("demo1").innerHTML = Fruits;
                  document.getElementById("demo2").innerHTML = Fruits.shift();
                  document.getElementById("demo3").innerHTML = Fruits;

                  </SCRIPT>
                  </BODY>
                  </HTML>
              Output:



























              5.  The unshift() Method: The unshift() method adds a new element to an array (at the beginning) and
                 shifts all other elements to a higher index. It returns the new length of the array. The unshift() method is
                 similar to the push() method but here the element added is the first one instead of the last and hence it is
                 slower. It also returns the number of items in the array.

              Example 50: To demonstrate the use of unshift() method

                  <!DOCTYPE HTML>
                  <HTML>
                  <HEAD>

                  <TITLE>unshift Array Method</TITLE>
                  </HEAD>
                  <BODY>
                  <H2>The unshift() Method</H2>
                  <P>Original Array: </P>

                  <P id="demo1"></P>
                  <P>unshift Element: </P>
                  <P id="demo2"></P>
                  <P>Array after unshift element: </P>

                  <P id="demo3"></P>

                240   Touchpad Web Applications (Ver. 2.0)-XII
   237   238   239   240   241   242   243   244   245   246   247