

includes() method and best of luck on your JavaScript journey.In order to know if a particular String contains a given word or a substring you can make use of the indexOf() method in JavaScript includes() method is case sensitive which means if the search-string doesn't match the exact casing in str then it will return false. If the search-string is not found then it will return false. If the search-string is found then it will return true.

str.includes(search-string, optional-position)

includes() method to see if one string is found in another. You can see that the (whole) word "love" is not present in that string. Position 3 until the end of the sentence includes these characters and spaces. This returns false because position 3 is the letter "o". "I love freeCodecamp".includes('love', 3) If we change the position to be 3, then the return would be false. "I love freeCodeCamp".includes('love', 1) This is what our code would look like using the position parameter. Remember that spaces in strings get an index value. Position 1 until the end of the sentence includes these characters and spaces. Our code will return true because when we start searching at position 1, the word "love" doesn't appear until position 2 so it's completely contained in the string. Remember that strings use zero-based indexing which means that the first letter "I" is index 0. We now want to check if "love" is found in "I love freeCodeCamp" when the search starts at position 1. We are going to modify our example to use the position parameter. How to use the optional position parameter If we modify our str to "I LOVE freeCodeCamp" and the search-string is still "love", then the return value would be false. Since the word "love" is included inside the str, then the code will return true. In the code, str would be "I love freeCodeCamp" and the search-string would be "love". We want to see if the word "love" is included in that sentence. In this first example, we have the sentence, "I love freeCodeCamp". If the position parameter is omitted then the default is zero. The position parameter is an optional number for the starting search position in the str. The search-string parameter is the string you are searching for in str. includes() method: str.includes(search-string, optional-position) In this article, I will walk you through a few code examples of the JavaScript string method called.
