1var string = "foo",
2 substring = "oo";
3
4console.log(string.includes(substring));
1// With ES6 MDN docs .includes()
2"FooBar".includes("oo"); // true
3"FooBar".includes("foo"); // false
4"FooBar".includes("oo", 2); // false (2 is the start position for the search)
5
6// E: Not suported by IE - instead you can use the Tilde opperator ~ (Bitwise NOT) with .indexOf()
7~"FooBar".indexOf("oo"); // -2
8~"FooBar".indexOf("foo"); // 0
9~"FooBar".indexOf("oo", 2); // 0 (parameter 2 is the start position for the search)
10
11// Used with a number, the Tilde operator effective does ~N => -(N+1). Use it with double negation !! (Logical NOT) to convert the numbers in bools:
12!!~"FooBar".indexOf("oo"); // true
13!!~"FooBar".indexOf("foo"); // false
14!!~"FooBar".indexOf("oo", 2); // false
1var str = "We got a poop cleanup on isle 4.";
2if(str.indexOf("poop") !== -1){
3 alert("Not again");
4}
5//use indexOf (it returns position of substring or -1 if not found)
1"FooBar".includes("oo"); // true
2
3"FooBar".includes("foo"); // false
4
5"FooBar".includes("oo", 2); // false
1if (your_string.indexOf('hello') > -1)
2{
3 alert("hello found inside your_string");
4}