contains function, that you may know from other languages, but it includes the
include function, which can be used to check if a string contains a substring.
includes method. You can either polyfill it or just use the
indexOf method, that’s also available on the String prototype.
const text = "codesnacks"; text.includes("snacks"); // => true text.includes("foo"); // => false text.indexOf("snacks"); // => 4 text.indexOf("code"); // => 0 text.indexOf("foo"); // => -1
positionof the first occurrence of the substring. If
textstarts with the substring the position will be
-1if the substring can’t be found
Interested in more content like this? Subscribe to my weekly Newsletter!