Javascript string contains6/18/2023 If you have any questions or anything you can find me on my Twitter, or you can read some of my other articles like Unlimited arguments in a JavaScript function in 2 ways. The includes() method provides cleaner code, but indexOf() returns information that includes() doesn’t. In the example above we can see the application of both methods. NOTE: includes() is not supported in Internet Explorer.Ĭonsole.log("A substring is close enough") Also, includes() accepts the same two parameters as indexOf(). Instead of returning index, includes() method returns true or false. The includes() method is a searching algorithm used to find a substring within a string or to find elements within an. The includes() method performs a case-sensitive search to determine whether one string may be found within another string, returning true or false as appropriate, according to MDN. Return true if the string in the first element of the array contains all of the letters of the string in the second element of the array. Check if JavaScript string contains a substring ES6Īmong other things, ES6 introduced includes() method. This concludes pre-ES6 way of checking if JavaScript String contains substring. That means that there was no occurrence of our string after the index we’ve provided. But, when we used indexOf() with the second parameter that is larger than 8, -1 was returned. This means that the first occurrence of our string is on the 8th index. As we can see from the example above when we used indexOf() without the second parameter indexOf() returned 8.
0 Comments
Leave a Reply. |