Back to questions
JuniorTheoryCommon
What does the program print?
const stack = [];
stack.push(1);
stack.push(2);
stack.push(3);
const a = stack.pop();
const b = stack.pop();
console.log(a, b);3 2
- Expecting
1 2:poptakes from the end of the array, so the last value pushed comes out first — a stack is last in, first out. - Expecting
3 3:popremoves the element it returns, so the secondpopsees[1, 2]and returns2.
- What does
stack.at(-1)return after the twopopcalls? - Which array method would take
1out first instead?