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

后端 未结 3 655
孤城傲影
孤城傲影 2021-02-05 06:57

I just want to confirm if the following two Javascript statements produces the same results, as it seems to me:

First:

var element = my_array.splice(0,1)         


        
3条回答
  •  执念已碎
    2021-02-05 07:23

    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.

提交回复
热议问题