We will learn how to check if string doesn’t include substring in JavaScript using two different methods. Both methods are straightforward, and you can choose whichever suits you most.
Check if string doesn’t include substring
Using includes()
Syntax:
includes(substring)
Parameter:
- substring: The substring to search for.
The includes() function returns a boolean value which answers whether a substring is in a string or not. The following example will check if the word “father” is included in the string “grandfather” or not:
let string = "grandfather";
let substring = "father";
if (string.includes(substring) == false)
console.log(string+" does not include " + substring );
else console.log("Found "+ substring + " in "+ string);
Output:
Found father in grandfather
The logic behind this method is straightforward. Because we want to check if a string doesn’t include a substring, the condition inside the if statement should be looking for a false boolean value because it indicates that the string does not include a substring we are searching for.
Using search()
Syntax:
search(substring)
Parameter:
- substring: The substring to search for.
The search() method returns the index of the first position the substring appeared in the string. If no match was found, it would return -1. We will check whether a -1 value is returned in the if condition:
let string = "grandfather";
let substring = "mother";
if (string.search(substring) == -1)
console.log(string+" does not include " + substring );
else console.log("Found "+ substring + " in "+ string);
As you want to check if string doesn’t include substring, your if statement conditional must equal with value -1 returned from the search() method. This implies that the string doesn’t include substring.
Output:
grandfather does not include mother
The search() method can search for a match between a regular expression (strong search values) and a String object. Moreover, this method is well-known for its effectiveness compared with the indexOf() method. The indexOf() method cannot take regular expressions. On the contrary, the search() method cannot take a second start position argument.
Summary
We have learned How To check if string doesn’t include substring in JavaScript using two different methods. It would help if you considered that each approach has its pros and cons. We can assume that the given substring is not included in the string by seeking a false boolean value returned by the two methods.
Maybe you are interested:
- Check if a String contains a Substring in JavaScript
- Replace all hyphens in a string in Javascript
- Remove all non-alphanumeric characters from string in JS

I’m Edward Anderson. My current job is as a programmer. I’m majoring in information technology and 5 years of programming expertise. Python, C, C++, Javascript, Java, HTML, CSS, and R are my strong suits. Let me know if you have any questions about these programming languages.
Name of the university: HCMUT
Major: CS
Programming Languages: Python, C, C++, Javascript, Java, HTML, CSS, R