And you're equipped with a handy mnemonic, that splice compared to slice has an additional letter, 'p', which helps you remember that splice mutates and optionally adds or removes from the original array. In JavaScript, array.splice() is a built-in function that modifies an array by adding new elements to it or removing/replacing the existing elements from it. You now know that slice makes a shallow copy of the original array, while splice mutates the original array and optionally adds or removes elements. ConclusionĪnd there we have it! This blog goes over the differences between slice and splice. And because splice can add and remove stuff to the original array, that means that it also mutates the original array. Because of the extra letter, I associate the additional letter to splice's use of adding or removing from the original array. splice has an extra letter, 'p', compared to slice. The JavaScript splice () method modifies an array. Spline is a free 3D design software with real-time collaboration to create web interactive experiences in the browser. I remember the difference between slice and splice using a mnemonic. Note: This method changes the original array. insert 'juliet' and 'zeke' at 3rd index // returns Ĭonsole. The splice() method adds/removes items to/from an array, and returns the removed item(s). splice ( 3, 1, 'juliet', 'zeke' ) // remove 'harper'.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |