• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / JavaScript / Check That String Does Not Contain Character in JavaScript

Check That String Does Not Contain Character in JavaScript

July 19, 2022 Leave a Comment

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.

Other Articles You'll Also Like:

  • 1.  How to Repeat Character N Times in JavaScript
  • 2.  How to Convert Degrees to Radians Using JavaScript
  • 3.  Using the getElementsByClassName Method in JavaScript
  • 4.  Using JavaScript to Get the Domain From URL
  • 5.  Changing the Background Image of a div in JavaScript
  • 6.  Finding the Length of a String in JavaScript
  • 7.  How to Iterate through an Array Using JavaScript
  • 8.  Using JavaScript to Run a Function Every 5 seconds
  • 9.  Using JavaScript to Get a Random Number Between Range of Numbers
  • 10.  JavaScript isInteger – How to Check if a Number is an Integer

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications 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 it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy