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. 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. I remember the difference between slice and splice using a mnemonic. insert 'juliet' and 'zeke' at 3rd index // returns Ĭonsole. Syntax of the Slice ( ) Method in Javascript: array.slice( start, end) This method extracts elements from the array and returns it just as an output. splice ( 3, 1, 'juliet', 'zeke' ) // remove 'harper'. Slice () Method in JavaScript extracts elements from the array and returns it as an output but it does not update or change to the array, the array remains the same as it was previously.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |