The slice( ) method copies a given part of an array and returns that copied part as a new array. It doesn’t change the original array.
array.slice(from, until);
From: Slice the array starting from an element index
Until: Slice the array until another element index
For example, I want to slice the first three elements from the array above. Since the first element of an array is always indexed at 0, I start slicing “from”0.
The slice( ) method doesn’t include the last given element.
The splice( ) method changes an array, by adding or removing elements from it
For removing elements, we need to give the index parameter, and the number of elements to be removed:
Index is the starting point for removing elements. Elements which have a smaller index number from the given index won’t be removed:
Adding Elements
For adding elements, we need to give them as the 3rd, 4th, 5th parameter (depends on how many to add) to the splice ( ) method:
array.splice(index, number of elements, element, element);
===========Split ( )================
Divides a string into substrings
Returns them in an array
Takes 2 parameters, both are optional: string.split(separator, limit)
Doesn’t change the original string
Can only be used for strings
-------Slice ( )------------
Copies elements from an array
Returns them as a new array
Doesn’t change the original array
Starts slicing from … until given index: array.slice (from, until)
Slice doesn’t include “until” index parameter
Can be used both for arrays and strings
--------Splice ( )-----------
Used for adding/removing elements from array
Returns an array of removed elements
Changes the array
For adding elements: array.splice (index, number of elements, element)
For removing elements: array.splice (index, number of elements)
Can only be used for arrays
--------Split ( )----------------]
Divides a string into substrings
Returns them in an array
Takes 2 parameters, both are optional: string.split(separator, limit)
Doesn’t change the original string
Can only be used for strings
==============slice( )=================
The slice( ) method copies a given part of an array and returns that copied part as a new array. It doesn’t change the original array.
array.slice(from, until); From: Slice the array starting from an element index Until: Slice the array until another element index For example, I want to slice the first three elements from the array above. Since the first element of an array is always indexed at 0, I start slicing “from”0.
The slice( ) method doesn’t include the last given element.
const myArr = [1,2,3]; const arrTwo = myArr.slice(1,2); console.log(arrTwo);
============== Splice( )=================
The splice( ) method changes an array, by adding or removing elements from it For removing elements, we need to give the index parameter, and the number of elements to be removed: Index is the starting point for removing elements. Elements which have a smaller index number from the given index won’t be removed:
const myArr = [1,2,3]; const arrTwo = myArr.splice(2,1); console.log(arrTwo); console.log(myArr);
Adding Elements For adding elements, we need to give them as the 3rd, 4th, 5th parameter (depends on how many to add) to the splice ( ) method:
array.splice(index, number of elements, element, element);
===========Split ( )================
Divides a string into substrings Returns them in an array Takes 2 parameters, both are optional: string.split(separator, limit) Doesn’t change the original string Can only be used for strings
-------Slice ( )------------
Copies elements from an array Returns them as a new array Doesn’t change the original array Starts slicing from … until given index: array.slice (from, until) Slice doesn’t include “until” index parameter Can be used both for arrays and strings
--------Splice ( )-----------
Used for adding/removing elements from array Returns an array of removed elements Changes the array For adding elements: array.splice (index, number of elements, element) For removing elements: array.splice (index, number of elements) Can only be used for arrays
--------Split ( )----------------]
Divides a string into substrings Returns them in an array Takes 2 parameters, both are optional: string.split(separator, limit) Doesn’t change the original string Can only be used for strings