![]() If indexStart or inde圎nd is less than 0, it is treated as 0.The substring() function has some quirky behavior in edge cases: str.substring( 5) // 'the night before Christmas' If you don't specify inde圎nd, the substring() function returns the rest of the string starting at indexStart. Str.substring(indexStart, inde圎nd) // 'Twas' ![]() For example: const str = 'Twas the night before Christmas' It returns the portion of the string that starts at indexStart and ends the character immediately preceding inde圎nd. It takes two parameters: indexStart and inde圎nd. The substring() function is the most common way to get a substring in JavaScript. In this tutorial, you'll learn the difference between these 3 ways to get a substring in JavaScript. There's also a third way to get a substring, the String#slice() function, that you may see in the wild. Even experienced JavaScript developers mix them up sometimes. The difference between the String#substring() and String#substr() functions is a common source of confusion.
0 Comments
Leave a Reply. |