

Ĭonsole.log(str.toLowerCase().includes("String".toLowerCase())) //trueĬonsole.log(str.toUpperCase().includes("String". Lu ý rng phng thc có phân bit ch hoa ch thng. Phng thc s tr v True nu chui cha chui con mà ngi dùng cung cp, ngc li s tr v False. One of the ways to solve the syntax error is to convert the string and the substring to the same case before searching. Hàm string.includes () trong Javascript Phng thc string.includes () s kim tra xem mt chui con c ngi dúng cung cp có nm trong chui hay không. However, it could not locate String in the string JavaScript string contains is easy, because it is case sensitive. The includes() method found the string substring in JavaScript string contains is easy. Will we locate the substring if we capitalize one of its characters?Ĭonsole.log(str.includes("String")) // false Yes, the str string contains the string substring.Įxample-4: Using various capitalization cases In JavaScript, the includes() method determines whether a string contains the given characters within it or not. Ĭonsole.log(str.includes("string", -3)) // true Here is an example using a negative index. Specifying a starting index less than the substring's leads to searching the substring throughout the entire string. But does it start at index 13?Įxample-3: Using a substring and a negative starting position It is true that the string substring starts at index 11 in the str string. Ĭonsole.log(str.includes("string", 11)) // true

We can also check whether the target substring is located at a specific index.

It is true that the string JavaScript string contains is easy contains the string substring.Įxample-2: Using a substring and a starting position

It is 0 by default.ĭifference between includes() and contains() It gives the position at which to start the search.
