How to check if a string contains a substring in JavaScript

Last updated on Sep 8, 2020 by Suraj Sharma

In this tutorial, you will learn how you can check whether a substring is included in a string or not.

I am going to discuss two built-in methods of JavaScript string, which helps us in determining whether a string is a substring of another string or not.

Built-in methods of JavaScript String

1. String.includes()

includes() method was introduced in ES6, since then it is a widely used string method. However, Internet Explorer and some other old browsers do not support the includes() method.

function isASubstring(largerString, smallerString) {
  return largerString.includes(smallerString);

console.log(isASubstring("You're awesome!", "some")) // true
console.log(isASubstring("You're awesome!", " ")) // true
console.log(isASubstring("You're awesome!", "nice")) // false

2. String.indexOf()

indexOf() method is supported in almost all the old browsers. indexOf returns the index of the first matched string. If a string is not a substring, then indexOf returns -1

function isASubstring(largerString, smallerString) {
  return largerString.indexOf(smallerString) > -1;

console.log(isASubstring("JavaScript", "Java")) // true
console.log(isASubstring("JavaScript", "React")) // false

Related Solutions

Rate this post

Suraj Sharma is a Full Stack Software Engineer. He holds a B.Tech degree in Computer Science & Engineering from NIT Rourkela.