We can use JavaScript to check that a string does not contain a specific character by using the JavaScript indexOf() method.

var someString = "Hello how are you today?"
var charCheck = someString.indexOf('x');
//If charCheck == -1, then the string does not contain the character

In our code above, if the variable charCheck is equal to -1, then it means the character x was not found in our string.

In this case, charCheck will equal -1 since the character x is not in our string.

Let’s put our code in a function to make checking that a string does not contain a character very simple.

function doesNotContainChar(str,char){
  if( str.indexOf(char) == -1 ) {
    return true;
  } else {
    return false;
  }
};

Our function takes two parameters. The first is the string we want to check. The second is the character that we want to make sure is not in the string.

If the character is not in the string, the function will return true. If the character is found in the string, then false will be returned.

Let’s see some examples of our function in use. We will use a long string containing text taken from our about page.

function doesNotContainChar(str,char){
  if( str.indexOf(char) == -1 ) {
    return true;
  } else {
    return false;
  }
};

var aboutPageString = "This blog is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes. Working with Python, Javascript, PHP, HTML, SAS, and other programming languages can allow us to create amazing programs which make our work more efficient, repeatable, and accurate. At the end of the day, we want to be able to just push a button and let the code do its magic. Thank you for reading!"

console.log(doesNotContainChar(aboutPageString,'h'));
console.log(doesNotContainChar(aboutPageString,'z'));
console.log(doesNotContainChar(aboutPageString,'0'));
console.log(doesNotContainChar(aboutPageString,'!'));
console.log(doesNotContainChar(aboutPageString,'Y'));


#Output:
false
false
true
false
true

Note that checking for characters is case-sensitive. So in our last example, while there is a lowercase y in our string, checking for an uppercase “Y” returns true because there is no uppercase Y in our string.

Check That String Does Not Contain Another String in JavaScript

We can also use our code above to check that a string does not contain another string.

var someString = "Hello how are you today?"
var stringCheck = someString.indexOf('tomorrow');

console.log(stringCheck);

#Output
-1

We can once again put this code into a function to simplify things.

function doesNotContainString(str,str2){
  if( str.indexOf(str2) == -1 ) {
    return true;
  } else {
    return false;
  }
};

And we can test our function with a couple of strings:

function doesNotContainString(str,str2){
  if( str.indexOf(str2) == -1 ) {
    return true;
  } else {
    return false;
  }
};

var aboutPageString = "This blog is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes. Working with Python, Javascript, PHP, HTML, SAS, and other programming languages can allow us to create amazing programs which make our work more efficient, repeatable, and accurate. At the end of the day, we want to be able to just push a button and let the code do its magic. Thank you for reading!"

console.log(doesNotContainString(aboutPageString,'hello'));
console.log(doesNotContainString(aboutPageString,'Python'));
console.log(doesNotContainString(aboutPageString,'python'));
console.log(doesNotContainString(aboutPageString,'Thank you'));

#Output:
true
false
true
false

Hopefully this article has been useful for you to learn how to use JavaScript to check is a string does not contain another character or string.

Categorized in:

JavaScript,

Last Update: April 2, 2024