js get substring before character 1

js get substring before character

const string = "0123456789";
console.log(string.slice(0, 2)); // "01"
console.log(string.slice(0, 8)); // "01234567"
console.log(string.slice(3, 7)); // "3456"

Here is what the above code is Doing:
1. We create a string variable called string and assign it the value “0123456789”.
2. We then use the slice() method to extract the first two characters from the string.
3. We then use the slice() method to extract the first eight characters from the string.
4. We then use the slice() method to extract the characters from the third to the seventh position.

Similar Posts