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

javascript
1const quote = “Heaven is under our feet as well as over our heads.; 2const text = “feet”; 3 4console.log(quote.includes(text)) //true 5console.log(quote.includes("heaven")) //false due case sensitive search 6console.log(quote.includes("Heaven",15)) //false 7console.log(quote.includes("Heaven")) //true 8 9//below snippet prints "found a match" 10if(quote.includes("heads")){ 11 console.log("found a match") 12} 13 14else{ 15 console.log("not found") 16} 17

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

javascript
1const quote = "A weed is no more than a flower in disguise." 2 3console.log(quote.indexOf("weed")) //2 4console.log(quote.indexOf("weed",3)) //-1 5console.log(quote.indexOf("tree")) //-1 6console.log(quote.indexOf("flower")) //25 7console.log(quote.indexOf("Flower")) //-1 8console.log(quote.indexOf("disguise",25)) //35

My other recent posts



About the Author

I am a Backend System Engineer at a credit card company, specializing in C/C++ and assembler on IBM's TPF OS. I have a passion for web development and enjoy working with Node.js and Python in my free time.

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