The array selection has a higher operator precedence than the increment by one, so first the item with index 2 from the array is selected, which happens to be 3; this is then incremented by one so you get 4.
2
Yup. That's exactly the case. If you write as this then it all make sense :
const a = [1,2,3] ++a[2]
There's a tiny mistake in your adaption: x++ increments x, but returns the previous value, whereas ++x increments x, but returns the new value.
x++
++x
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
The array selection has a higher operator precedence than the increment by one, so first the item with index
2from the array is selected, which happens to be 3; this is then incremented by one so you get 4.Yup. That's exactly the case.
If you write as this then it all make sense :
There's a tiny mistake in your adaption:
x++increments x, but returns the previous value, whereas++xincrements x, but returns the new value.