skip to Main Content
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


  1. The .pop() method will always work (if this has a length property). There’s no error or exception thrown by this method.

    The pop() method of Array instances removes the last element from an array and returns that element. This method changes the length of the array.

    The pop() method removes the last element from an array and returns that value to the caller. If you call pop() on an empty array, it returns undefined.

    From: Description

    See how it perfectly works:

    const originalArray = [0, 1, 2, 3, 4];
    
    const lastValue = originalArray.pop();
    
    console.log
    ({
      'The original array': originalArray,
      'The last value extracted from the array': lastValue,
    });

    The pop() method is a mutating method. It changes the length and the content of this. In case you want the value of this to be the same, but return a new array with the last element removed, you can use [...].slice(0, -1) instead.

    From: Description

    const originalArray = [0, 1, 2, 3, 4];
    
    const lastValue = originalArray.at(-1);
    const modifiedArray = originalArray.slice(0, -1);
    
    console.log
    ({
      'The original array': originalArray,
      'The modified array': modifiedArray,
      'The last value extracted from the array': lastValue,
    });
    .as-console-wrapper { min-height: 100%; }

    IMPORTANT: If you’re dealing with arguments in some functions, there is a very peculiar distinction though… For example:

    const originalArguments = [ 0, 1, 2, 3, 4 ];
    
    function anyWrapper ( ... receivedArguments )
    {
      const internalArguments = originalArguments;
      const internalLastValue = internalArguments.pop();
      const originalLastValue = originalArguments.pop();
      const receivedLastValue = receivedArguments.pop();
      
      console.log
      (
        Object.is(internalArguments, originalArguments)
        ? '`internalArguments` is the same as `originalArguments`.'
        : '`internalArguments` is NOT the same as `originalArguments`.'
      );
      
      console.log
      (
        Object.is(receivedArguments, originalArguments)
        ? '`receivedArguments` is the same as `originalArguments`.'
        : '`receivedArguments` is NOT the same as `originalArguments`.'
      );
      
      console.log
      (
        Object.is(arguments, originalArguments)
        ? '`arguments` is the same as `originalArguments`.'
        : '`arguments` is NOT the same as `originalArguments`.'
      );
      
      console.log
      ({
        'Internal Last Value': internalLastValue,
        'Original Last Value': originalLastValue,
        'Received Last Value': receivedLastValue,
        'The Arrays States': {originalArguments, receivedArguments, internalArguments},
      });
    }
    
    anyWrapper.apply(null, originalArguments);
    .as-console-wrapper { min-height: 100%; }

    NOTE that the internalArguments is the same as originalArguments, but receivedArguments is not.

    Login or Signup to reply.
  2. 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]

    let channels_history = [0, 0, 1, 0];
    let channel = 0;
    
    
    channel = channels_history.pop(); // here you mutate your original array
    console.log(channels_history);

    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

    let channels_history = [0, 0, 1, 0];
        let channel = 0;
    
        
        channel = channels_history.pop(); // here you mutate your original array 
        console.log(channels_history.pop()); // here you console.log(the last element of mutated array
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search