let channels_history = [0, 0, 1, 0];
let channel = 0;
// wrapped in some button click event handler >>>>>
channel = channels_history.pop();
console.log(channels_history);
which outputs
[0, 0, 1, 0]
However, if I do
let channels_history = [0, 0, 1, 0];
console.log(channels_history.pop());
it works, and I get the desired values.
2
Answers
The
.pop()
method will always work (ifthis
has alength
property). There’s no error or exception thrown by this method.From: Description
See how it perfectly works:
From: Description
IMPORTANT: If you’re dealing with arguments in some functions, there is a very peculiar distinction though… For example:
NOTE that the
internalArguments
is the same asoriginalArguments
, butreceivedArguments
is not.We do get the expected result as described in MDN
In your first code you pop() once your original array that you console.log so you get [0, 0, 1]
in your second code you pop() once your original array then you console.log your mutated array.pop() which is the last value then which is 1