In this blog post you’ll learn the JavaScript includes() method. Method to check if specified string contains in a string or not.
Description:
includes() method returns true if the given string or value found in string otherwise it returns false.
Includes() Method is case sensitive.
Syntax:
JavaScript
string.includes(Searchvalue, start)
Parameter Description:
Searchvalue: The value to search in string. (Required)
start: Index position to start searching from. Default is 0 (Optional)
Example 1: Check the specific words present in string or not.
Includes() method returns true if the given word present in string otherwise return false.
JavaScript
let text = "Hello Good morning!. Welcome to maketechstuff.com";
document.write(fruit_array.includes("Good"));
// Output:
// true
Searching from a different position (Index position).
JavaScript
let text = "Hello Good morning!. Welcome to maketechstuff.com";
document.write(text.includes("Good", 10));
// Output:
// false
Example 2: Check for specific array elements in array.
You can check the presence of specific array element in array using includes() method.
JavaScript
let fruit_array = ["Mango", "Orange", "Banana", "Apple", "Guava", "Grapes"];
document.write(fruit_array.includes("Orange"));
// Output:
// true
So that's how JavaScript includes() method works. If you have any query or suggestion feel free to write them in the comment section.
Share your thoughts.