Equal values , different outputs ? array.shift()

Hello everyone,

So , this should be a very simple question.

I have set this variable named ShortGroceryList = groceryList.shift(). I was expecting the same value to be the output when I log both in different console.log statement but that’s not what happens,

The output on the screen goes like this :

orange juice when I log => console.log(ShortgroceryList)

and

bananas when I log =>console.log(groceryList.shift())

why do you expect the same output? Each time you call shift, the first element is removed from the array. you call .shift() twice.

2 Likes

Remember that .shift() removes the first element of an array.

Welcome to the forums!

1 Like

Hey ! Thank you for the quick response guys .

I see , so that means this method cuts a piece of the array each time I call him , right ?

If i call it 3 times he will cut off the first 3 elements of the array . Correct?

yes :slight_smile:

If the working of a method or function is unclear, there is always documentation. For JS, MDN is good, so if i google:

javascript shift

i get:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/shift

as first search result. Which includes explanation and example.

2 Likes