revision:
The includes() method determines whether an array includes a certain value among its entries, returning true or false as appropriate.
includes(searchElement) includes(searchElement, fromIndex)
Parameters:
searchElement : the value to search for.
fromIndex : optional. Zero-based index at which to start searching, converted to an integer. Negative index counts back from the end of the array — if "fromIndex < 0", "fromIndex + array.length" is used. However, the array is still searched from front to back in this case. If "fromIndex < -array.length" or "fromIndex is omitted", 0 is used, causing the entire array to be searched. If "fromIndex >= array.length", the array is not searched and false is returned.
const array1 = [1, 2, 3]; console.log(array1.includes(2)); // Expected output: true const pets = ['cat', 'dog', 'bat']; console.log(pets.includes('cat')); // Expected output: true console.log(pets.includes('at')); // Expected output: false
The includes() method returns true if a string contains a specified string. Otherwise it returns false. The includes() method is case sensitive.
string.includes(searchvalue, start)
Parameters:
searchvalue : required. The string to search for.
start : optional. The position to start from. Default value is 0.
<p>includes() returns true if a string contains a specified string.</p> <p>Find "world":</p> <p id="demo"></p> <script> let text = "Hello world, welcome to the universe."; let result = text.includes("world"); document.getElementById("demo").innerHTML = result; </script>
<div> <p id="include-1"></p> <p id="include-2"></p> <p id="include-3"></p> <p id="include-4"></p> <p id="include-5"></p> <p id="include-6"></p> </div> <script> console.log([1, 2, 3].includes(2)); // true console.log([1, 2, 3].includes(4)); // false console.log([1, 2, 3].includes(3, 3)); // false console.log([1, 2, 3].includes(3, -1)); // true console.log([1, 2, NaN].includes(NaN)); // true console.log(["1", "2", "3"].includes(3)); // false document.getElementById("include-1").innerHTML = "[1, 2, 3].includes(2) : " + ([1, 2, 3].includes(2)); document.getElementById("include-2").innerHTML = "[1, 2, 3].includes(4) : " + ([1, 2, 3].includes(4)); document.getElementById("include-3").innerHTML = "[1, 2, 3].includes(3,3) : " + ([1, 2, 3].includes(3,3)); document.getElementById("include-4").innerHTML = "[1, 2, 3].includes(3, -1) : " + ([1, 2, 3].includes(3, -1)); document.getElementById("include-5").innerHTML = "[1, 2, NaN].includes(NaN) : " + ([1, 2, NaN].includes(NaN)); document.getElementById("include-6").innerHTML = '["1", "2", "3"].includes(3) : ' + (["1", "2", "3"].includes(3)); </script>
<div> <p id="include-7"></p> <p id="include-8"></p> <p id="include-9"></p> </div> <script> const arr = ["a", "b", "c"]; arr.includes("c", 3); // false arr.includes("c", 100); // false document.getElementById("include-7").innerHTML = 'arr.includes("c",3) : ' + arr.includes("c",3); document.getElementById("include-8").innerHTML = 'arr.includes("c",100) : ' + arr.includes("c",100); document.getElementById("include-9").innerHTML = 'arr.includes("c",2) : ' + arr.includes("c",2); </script>
<div> <p id="include-10"></p> <p id="include-11"></p> <p id="include-12"></p> <p id="include-13"></p> </div> <script> // array length is 3 // fromIndex is -100 // computed index is 3 + (-100) = -97 const arr1 = ["a", "b", "c"]; console.log(arr1.includes("a", -100)); // true console.log(arr1.includes("b", -100)); // true console.log(arr1.includes("c", -100)); // true console.log(arr1.includes("a", -2)); // false document.getElementById("include-10").innerHTML = 'arr1.includes("a", -100) : ' + arr1.includes("a", -100); document.getElementById("include-11").innerHTML = 'arr1.includes("b", -100) : ' + arr1.includes("b", -100); document.getElementById("include-12").innerHTML = 'arr1.includes("c", -100) : ' + arr1.includes("c", -100); document.getElementById("include-13").innerHTML = 'arr1.includes("a", -2) : ' + arr1.includes("a", -2); </script>
<div> <p id="include-14"></p> <p id="include-15"></p> <p id="include-16"></p> <p id="include-17"></p> </div> <script> const fruits = [" Banana", " Orange", " Apple", " Mango"]; document.getElementById("include-14").innerHTML = "fruit array :" + fruits; document.getElementById("include-15").innerHTML = " fruit array includes : " + fruits.includes(" Mango"); document.getElementById("include-16").innerHTML = " fruit array includes : " + fruits.includes(" Banana", 3); document.getElementById("include-17").innerHTML = " fruit array includes : " + fruits.includes(" Banana", 0); </script>
<div> <p id="include-18"></p> <p id="include-19"></p> <p id="include-20"></p> <p id="include-21"></p> <p id="include-22"></p> <p id="include-23"></p> <p id="include-24"></p> </div> <script> var mystr = "Hello, world!"; document.getElementById("include-18").innerHTML = "string : " + mystr; var res1 = mystr.includes("Hello"); var res2 = mystr.includes("Ohayo"); var res3 = mystr.includes("e"); var res4 = mystr.includes("Hello", 1); var res5 = mystr.includes("ello", 1); var res6 = mystr.includes("l", 3); console.log(res1); console.log(res2); console.log(res3); console.log(res4); console.log(res5); console.log(res6); document.getElementById("include-19").innerHTML = "string includes 'Hello' : " + res1; document.getElementById("include-20").innerHTML = "string includes 'Ohayo' : " + res2; document.getElementById("include-21").innerHTML = "string includes 'e' : " + res3; document.getElementById("include-22").innerHTML = "string includes 'Hello', 1 : " + res4; document.getElementById("include-23").innerHTML = "string includes 'ello', 1 : " + res5; document.getElementById("include-24").innerHTML = "string includes 'l', 3 : " + res6; </script>