This article will see how to check if a substring is present in a string through different methods and various examples.

Table of Contents:-

Javascript check if string contains substring using includes()

Javascript includes() method is used to perform a case-sensitive search to find out if a particular string is contained within another string. The includes() method returns true if the substring is found else, it will return false.

Syntax:-

includes(substringToBeSearched)
includes(substringToBeSearched, position)
  • substringToBeSearched: is the substring to be searched.
  • position: is the integer value to be passed as an index position from where the search will start in the original string.

Example:-

We will be checking if the substring ‘merry’ is contained in the string ‘We wish you a merry christmas’

let mainString = "We wish you a merry christmas"
let subString = 'merry'
if(mainString.includes(`${subString}`))
{
console.log(`${subString} is included in string : ${mainString}`)
}
else {
 console.log(`${subString} is not included in string : ${mainString}`)
}

Output:-

merry is included in string : We wish you a merry christmas

Since the search is case-sensitive, changing the case will eventually lead to substring not found, for example. We will be checking if the substring ‘Merry’ is contained in the string ‘We wish you a merry christmas’

console.log('We wish you a merry christmas'.includes('Merry'))

Output:-

false

Example:-

Let us see another example with both the arguments passed. We will be checking if the substring ‘merry’ is contained in the string ‘We wish you a merry christmas’ after position 4.

let position = 4
let mainString = "We wish you a merry christmas"
let subString = 'merry'
if(mainString.includes(`${subString}`,position))
{
console.log(`${subString} is included in string : ${mainString} after position ${position}`)
}
else{
console.log(`${subString} is not included in string : ${mainString} after position ${position} `)
}

Output:-

merry is included in string : We wish you a merry christmas after position 4

We will be checking if the substring ‘merry’ is contained in the string ‘We wish you a merry christmas’ after position 18.

let position = 18
let mainString = "We wish you a merry christmas"
let subString = 'merry'
if(mainString.includes(`${subString}`,position))
{
console.log(`${subString} is included in string : ${mainString} after position ${position}`)
}
else{
console.log(`${subString} is not included in string : ${mainString} after position ${position} `)
}

Output:-

merry is not included in string : We wish you a merry christmas after position 18 

Javascript check if string contains substring using indexOf()

In Javascript indexOf() method is used to return the index of the first occurrence of specified string within a string object from where it is called.

Syntax:-

indexOf(substringToBeSearched)
indexOf(substringToBeSearched, fromIndexPosition)
  • substringToBeSearched: is the substring to be searched.
  • fromIndexPosition: The indexOf() method will search for the particular substring staring with fromIndexPosition.

Example:-

We will be checking if the substring ‘merry’ is contained in the string ‘We wish you a merry christmas’

let mainString = "We wish you a merry christmas"
let subString = 'merry'
if(mainString.indexOf(`${subString}`) > 0 )
{
console.log(`${subString} is included in string : ${mainString}`)
}
else{
console.log(`${subString} is not included in string : ${mainString}`)
}

Output:-

merry is included in string : We wish you a merry christmas

We will be checking if the substring ‘other’ is contained in the string ‘We wish you a merry christmas’

let mainString = "We wish you a merry christmas"
let anyOtherString = 'other'
if(mainString.indexOf(`${anyOtherString}`) > 0 )
{
console.log(`${anyOtherString} is included in string : ${mainString}`)
}
else{
console.log(`${anyOtherString} is not included in string : ${mainString}`)
}

Output:-

other is not included in string : We wish you a merry christmas

Example:-

Let us see another example with both the arguments passed.

//search will start from position 10 and return the index of the first occurrence of 'merry' from the entire string 
console.log('We wish you a merry christmas'.indexOf('merry',10)) 
//search will start from position 18 and return the index of the first occurrence of 'merry' from the entire string
console.log('We wish you a merry christmas'.indexOf('merry',18)) 

Output:-

14
-1

Since the search started from the 18th position in the second javascript statement, ‘merry’ was not found, and hence -1 got returned as output.

We hope this article helped you to check if a substring is present within a string in javascript. Good Luck !!!