OneBite.Dev - Coding blog in a bite size

slice a string in Javascript

Code snippet on how to slice a string in Javascript

const str = 'Hello world';
const slicedStr = str.slice(6);

The above code slices a string. In the first line, a string Hello world is assigned to a variable str. In the second line, the slice() method is used on variable str, which will take out a part of the string and returns it. The slice() method takes an argument which is the index from which the slicing should start, in this example the slicing starts from index 6 and takes the remaining part of the string (which is ‘world’). The result is then stored in a variable slicedStr.

javascript