Javascript Array Tutorial - pop and shift method


Javascript provides to methods to remove elements from an array pop and shift. The pop method removes the element at the end of the array. So if we have

var x = ['toyota','honda','Audi'];

And if we use

x.pop ;

the resulting array will be

var x = ['toyota','honda'];

The shift methods removes one element from the begining of the array. Take a look at the complete example


<html>
<body>

<script type="text/javascript">

var x = ['toyota','honda','Audi'];

x.shift();

for (i=0; i<=1; i++)
document.writeln('x['+i+'] = '+ x[i]+ '<br />');

</script>

</body>
</html>



You may like to try this example online here .

It gives the following output

x[0] = honda
x[1] = Audi