Javascript “shift” versus “splice” - are these statements equal?

这一生的挚爱 提交于 2019-12-03 10:06:15

They will have the same effect, yes. splice(0, 1) will remove the first element from my_array and return a new array containing that element. shift will do the same, but return the element itself, not an array.

shift is more readable (in my opinion) and is also significantly faster (in Chrome at least):

Maxim Krizhanovsky

Both lines of code remove the first element from the array, and return the removed element, they are both supported in all major browsers.

You should use the second one, and the code will be more readable indeed.

shift returns the element that was removed, splice returns an array of elements that were removed.

that being said, the two statements do the same thing and i would agree that the second is more readable.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!