Search code examples
javascriptarraysstack

array.length-=1 vs array.pop()


I know that array is some kind of an object, but it also has numeric indexes. And arr.length is a property, which returns not the number of elements in the array, but the last index+1. We can remove the last element using decrement of length or function pop(). And the question is: What's the difference between these methods?


Solution

  • Some differences:

    • pop returns the value of the entry that you're removing, assigning to length doesn't.

    • pop is a method call; assigning to length is an assignment operation.

    • pop on an array whose length is 0 returns undefined and doesn't change the array. array.length -= 1 on an array with a length of 0 causes an error.