How to make a array as a stack using JavaScript?

Q

How to make a array as a stack using JavaScript?

✍: Guest

A

The pop() and push() functions turn a harmless array into a stack:

<script type="text/javascript">
var numbers = ["one", "two", "three", "four"];
numbers.push("five");
numbers.push("six");
document.write(numbers.pop());
document.write(numbers.pop());
document.write(numbers.pop());
</script>

This produces:
sixfivefour

2011-07-26, 2953👍, 0💬