ECMAScript 6 introduced String.prototype.includes:

const string = "foo";
const substring = "oo";

console.log(string.includes(substring)); // true

String.prototype.includes is case-sensitive and is not supported by Internet Explorer without a polyfill.

In ECMAScript 5 or older environments, use String.prototype.indexOf, which returns -1 when a substring cannot be found:

var string = "foo";
var substring = "oo";

console.log(string.indexOf(substring) !== -1); // true

🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › String › includes
String.prototype.includes() - JavaScript | MDN
You can work around this constraint by transforming both the original string and the search string to all lowercase: ... const str = "To be, or not to be, that is the question."; console.log(str.includes("To be")); // true console.log(str.includes("question")); // true console.log(str.includes("nonexistent")); // false console.log(str.includes("To be", 1)); // false console.log(str.includes("TO BE")); // false console.log(str.includes("")); // true
🌐
W3Schools
w3schools.com › jsref › jsref_includes.asp
JavaScript String includes() Method
More examples below. The includes() method returns true if a string contains a specified string.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-string-includes-method
JavaScript String includes() Method - GeeksforGeeks
Returns true if the value is present. Returns false if the value is not found. [Example 1]: The code checks whether "Geeks" exists in "Welcome to GeeksforGeeks." and logs true because the substring is found.
Published   March 22, 2018
🌐
TechOnTheNet
techonthenet.com › js › string_includes.php
JavaScript: String includes() method
In this example, we have declared a variable called totn_string that is assigned the string value of 'TechOnTheNet'. We have then invoked the includes() method of the totn_string variable to determine if a substring is found within totn_string.
🌐
Programiz
programiz.com › javascript › library › string › includes
Javascript String includes()
Here, str is a string. ... position (optional) - The position within str to begin searching for searchString. By default, it is 0. Returns true if searchString is found anywhere within str. Returns false if searchString is not found anywhere within str. Note: The includes() method is case sensitive. let sentence = "Java is to JavaScript what Car is to Carpet.";
🌐
Medium
medium.com › nerd-for-tech › basics-of-javascript-string-includes-method-107b6094f00b
Basics of Javascript · String · includes() (method) | by Jakub Korch | Nerd For Tech | Medium
June 4, 2021 - We are checking if a string with a smiley face contains a string with a smiley face. Obviously the result is true. The only difference is how I specified the string to look for. In the first case I used emoji directly. In the second case, I used unicode syntax with the prefix ‘\u’. However, for Javascript, it’s the same thing.
Top answer
1 of 16
418

You can use the .some method referenced here.

The some() method tests whether at least one element in the array passes the test implemented by the provided function.

// test cases
const str1 = 'hi hello, how do you do?';
const str2 = 'regular string';
const str3 = 'hello there';

// do the test strings contain these terms?
const conditions = ["hello", "hi", "howdy"];

// run the tests against every element in the array
const test1 = conditions.some(el => str1.includes(el));
const test2 = conditions.some(el => str2.includes(el));
// strictly check that contains 1 and only one match
const test3 = conditions.reduce((a,c) => a + str3.includes(c), 0) == 1;

// display results
console.log(`Loose matching, 2 matches "${str1}" => ${test1}`);
console.log(`Loose matching, 0 matches "${str2}" => ${test2}`);
console.log(`Exact matching, 1 matches "${str3}" => ${test3}`);

Also, as a user mentions below, it is also interesting to match "exactly one" appearance like mentioned above (and requested by OP). This can be done similarly counting the intersections with .reduce and checking later that they're equal to 1.

2 of 16
80

With includes(), no, but you can achieve the same thing with REGEX via test():

var value = /hello|hi|howdy/.test(str);

Or, if the words are coming from a dynamic source:

var words = ['hello', 'hi', 'howdy'];
var value = new RegExp(words.join('|')).test(str);

The REGEX approach is a better idea because it allows you to match the words as actual words, not substrings of other words. You just need the word boundary marker \b, so:

var str = 'hilly';
var value = str.includes('hi'); //true, even though the word 'hi' isn't found
var value = /\bhi\b/.test(str); //false - 'hi' appears but not as its own word
Find elsewhere
🌐
JavaScript Tutorial
javascripttutorial.net › home › javascript string methods › string.prototype.includes()
JavaScript String includes() Method
November 3, 2024 - In this tutorial, you will learn how to use the JavaScript String includes() method to check if a string contains a substring.
🌐
SheCodes
shecodes.io › athena › 220596-how-to-use-the-includes-method-in-javascript
[JavaScript] - How to use the .includes() method in | SheCodes
Learn how to use the `.includes()` method in JavaScript to check if a specific string or element is present in another string or array.
🌐
Reality Ripple
udn.realityripple.com › docs › Web › JavaScript › Reference › Global_Objects › String › includes
String.prototype.includes() - JavaScript
if (!String.prototype.includes) { String.prototype.includes = function(search, start) { 'use strict'; if (search instanceof RegExp) { throw TypeError('first argument must not be a RegExp'); } if (start === undefined) { start = 0; } return this.indexOf(search, start) !== -1; }; } const str = 'To be, or not to be, that is the question.' console.log(str.includes('To be')) // true console.log(str.includes('question')) // true console.log(str.includes('nonexistent')) // false console.log(str.includes('To be', 1)) // false console.log(str.includes('TO BE')) // false console.log(str.includes('')) // true
🌐
Vultr Docs
docs.vultr.com › javascript › standard-library › String › includes
JavaScript String includes() - Check Substring Presence | Vultr Docs
November 14, 2024 - ... var greeting = "Hello, how are you?"; if (greeting.includes("Hello")) { console.log("The greeting is polite."); } else { console.log("The greeting is not polite."); } Explain Code
🌐
Tabnine
tabnine.com › home › how to use the includes() method in javascript
How to Use the includes() Method in JavaScript - Tabnine
July 25, 2024 - This is why blank spaces count as part of the pattern to detect, and why it is possible to search for fractions of words – or even single characters, as seen below: console.log(str.includes('u')); // Expected output: true console.log(str....
🌐
freeCodeCamp
freecodecamp.org › news › javascript-string-contains-how-to-use-js-includes
JavaScript String Contains – How to use JS .includes()
November 5, 2021 - Position 3 until the end of the sentence includes these characters and spaces. ... You can see that the (whole) word "love" is not present in that string. In JavaScript you can use the .includes() method to see if one string is found in another.
🌐
Mimo
mimo.org › glossary › javascript › includes-method
JavaScript includes() method: Syntax, Usage, and Examples
Whether you're scanning for keywords, checking roles, or validating form fields, includes in JavaScript keeps your logic concise and expressive. Returns a Boolean: The primary purpose of includes() is to return a simple true or false, making it perfect for if statements. Works on Both Strings and Arrays: You can use the same method name for checking for substrings in strings and elements in arrays.
🌐
TutorialsPoint
tutorialspoint.com › home › javascript › javascript string includes method
JavaScript String Includes Method
September 1, 2008 - For example: "hi how are you".includes("Hi") method returns 'false'. <html> <head> <title>JavaScript String includes() Method</title> </head> <body> <script> const str = "hi how are you"; const searchString = "Hi"; document.write("Original string: ...
🌐
Scaler
scaler.com › home › topics › javascript string includes() method
JavaScript String includes() Method - Scaler Topics
May 4, 2023 - There are no exceptions in the string includes() function in javascript. Here in this example, we will make a string "s1" and then by using the includes() method, we will find if the string "SCALER" is present in the string s1 or not.
🌐
Codedamn
codedamn.com › news › javascript
JavaScript includes method for String and Array with Examples
June 2, 2023 - In this blog post, we will explore the includes() method in JavaScript, which provides an efficient way to accomplish this task for both strings and arrays. We will also discuss several usage examples to help you understand how to implement this method in your own projects on codedamn.
🌐
Sentry
sentry.io › sentry answers › javascript › how to check whether a string contains a substring in javascript?
How to Check Whether a String Contains a Substring in JavaScript? | Sentry
You want to check whether a string contains a substring in JavaScript. What are the different ways to do this? There are a number of ways you could approach this problem. We’ll take a look at two methods: includes() and indexOf().