Page 241 - Web_Application_v2.0_C12_Fb
P. 241
<SCRIPT>
const Fruits = ['Apple', 'Mango', 'Banana', 'Papaya'];
document.getElementById("demo1").innerHTML = Fruits;
document.getElementById("demo2").innerHTML = Fruits.push('Orange');
document.getElementById("demo3").innerHTML = Fruits;
</SCRIPT>
</BODY>
</HTML>
Output:
4. The shift() Method: The shift() method removes the first element from an array and returns it. This
method shifts all other elements in the array to a lower index, effectively reducing the length of the array.
The shift() method is similar to the pop() method but here the element removed is the first one instead of
the last and hence it is slower. It also returns the removed items.
Example 49: To demonstrate the use of shift() method
<!DOCTYPE HTML>
<HTML>
<HEAD>
<TITLE>shift Array Method</TITLE>
</HEAD>
<BODY>
<H2>The shift() Method</H2>
<P>Original Array: </P>
<P id="demo1"></P>
<P>Shift element: </P>
<P id="demo2"></P>
<P>Array after using shift method: </P>
<P id="demo3"></P>
JavaScript Part 2 239

