How to check if a string contains a substring or not in JavaScript?

Published on 2021-08-08, by Javed Shaikh

Subscribe for new article
*No spam. unsubscribe at anytime

Sometimes when you are building an app in JavaScript, you may come across a situation where you would like to check if a string contains a substring or not. Thankfully we have inbuilt functions to do this job. There are two methods in JavaScript to find if a string contains another string or not such as includes() and indexOf() methods.

The includes() method returns true or false after a search and indexOf() method returns index of a string or -1 if it does not find a match.

Here we will learn about two such methods with examples and see how they work.

  • includes ()
  • indexOf()

String.prototype.includes()

This method checks calling string and performs a scan to see if it contains the another string.This method returns either true or false. If a match is found, it returns true else it returns false. The search performed is case sensitive.

  • It performs case sensitive search
  • It returns true if match is found else it returns false
  • It may not be available in all browsers as it was added in ECMAScript 2015
  • It also has second optional argument i.e index/position where the search should begin. default is 0 (from beginning)

EXAMPLE

const quote = “Heaven is under our feet as well as over our heads.”;
const text = “feet”;

console.log(quote.includes(text))     //true
console.log(quote.includes("heaven")) //false due case sensitive search
console.log(quote.includes("Heaven",15))  //false
console.log(quote.includes("Heaven"))     //true

//below snippet prints "found a match"
if(quote.includes("heads")){
  console.log("found a match")
}

else{
  console.log("not found")
}

String.prototype.indexOf()

This method performs a search to check if a string contains another string and returns its index if a match is found. If it does not find a match then it returns -1. This method also does case sensitive search.

  • This method also performs case sensitive search
  • It returns index of first a sub-string on its first occurrence
  • It returns -1 if no match is found
  • It also has second optional argument i.e. index from where the search should begin.

EXAMPLE

const quote = "A weed is no more than a flower in disguise."

console.log(quote.indexOf("weed"))        //2
console.log(quote.indexOf("weed",3))      //-1
console.log(quote.indexOf("tree"))        //-1
console.log(quote.indexOf("flower"))      //25
console.log(quote.indexOf("Flower"))      //-1
console.log(quote.indexOf("disguise",25)) //35

My other recent posts



About the Author

I am a backend system engineer working for a credit card issuer. I mostly work on C/C++ and assembler programs on IBM's TPF OS. However I love web development and keep trying my hands on Nodejs and Python when offwork

Connect with author

Related articles ...

How to use localStorage and sessionStorage in JavaScript?

World Wide Web Consortium or W3C introduced two storage object mechanisms to store data in the browser which are localStorage and sessionStorage objects

2021-07-28

What is http cookie and how it works?

The HTTP protocol is stateless which means that the server does not remember about any response it sent to the user’s browser. It executes each request independently without knowing past requests that are already executed.

2021-07-26

How to check if a string is null, blank, empty or undefined using JavaScript?

Instead of using any library function, in this post we are going to learn how to check if a string is null, blank, empty or undefined with just one line instruction

2021-08-10