The changes to the text in one string do not affect the other string.
The syntax for string slice method is following.
// app.js let str = 'Obama was US president'; let result = str.slice(2, 9); console.log(result);
Using slice() method with negative indexes
The following example uses slice() methods with negative indexes.
// app.js let str1 = 'Trump is current US president'; let r1 = str1.slice(-6, -1); console.log(r1);
Pass only one parameter index
If we pass only startIndex, then it will extract the part of the string from the startIndex to the end of the string.
// app.js let str1 = 'Trump is current US president'; let r1 = str1.slice(1); console.log(r1);
Extract the whole string:
// app.js let str2 = "Hakuna Matata"; let res2 = str2.slice(0); console.log(res2);
Extract only the last character.
// app.js let str3 = "Hakuna Matata"; let res3 = str3.slice(-1); console.log(res3);