Basic JavaScript Question:
How to make a array as a stack using JavaScript?
Answer:
The pop() and push() functions turn a harmless array into a stack in JavaScript...
<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
<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
Previous Question | Next Question |
How to use "join()" to create a string from an array using JavaScript? | How to shift and unshift using JavaScript? |