The slice
method can be used for an Array
or a String
variable. The intention of slice
method is to slice out some part of the data.
For example, if I have a string
let word = 'India';
And I want to extract dia
from it, we can do it using –
let dia = word.slice(2); //output > dia,
This will slice the string from index 2
to end of the string.
If we want to slice the middle part of the string then we can do something like –
let di = word.slice(2, 4); //output > di
The same way we can process Array as well.
solved Adding a slice function to a string