Navigate back to the homepage

How to check if a string is a substring of another string in javascript?

Infinitbility
JavaScript
May 25th, 2022 · 1 min read

Hi Friends 👋,

Welcome To Infinitbility! ❤️

Today, I’m going to show you how do you check if a string is a substring of another string in javascript, here I will use the javascript includes() method to check contains a substring or not.

The includes() method determines whether an array includes a certain value among its entries, returning true or false as appropriate.

Let’s start the today’s tutorial How to check if a string is a substring of another string in javascript?

To check string contains a specific word or not, we have to just pass the word in the includes() method, and then it will return true or false.

Here, we will do

  1. Create a variable and assign some value to it
  2. Use the includes() method to check string contains sub string or not
  3. Console output substring contain or not
1// Create a variable and assign some value on it
2const slogan = "We have the ability to build infinite way for us.";
3
4// Use the `includes()` method to check string contain sub string or not
5// Console output substring contain or not
6if(slogan.includes("infinite")){
7 console.log("SubString contain");
8} else {
9 console.log("SubString not contain");
10}

When running the above program it should console 'SubString contain' in the log. let’s check the output.

javascript, check if a string is a substring of another string example
javascript, check if a string is a substring of another string example

I hope it’s help you, All the best 👍.

Follow me on Twitter

Join our email list and get notified about new content

No worries, I respect your privacy and I will never abuse your email.

Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.

Looking For React Native Tutorial?

we are trying to create the best Tutorial for react native developers.

When you want a daily updates about React Native Tutorial or infinitbility update subscribe to our newsletter.

Read React Native Tutorial

Request New Tutorial or Article on mail [email protected]

Guide book

Guidebook: The Javascript Monorepo Photoshop Guide

Tutorials

Categories

More articles from Infinitbility

How to check if a string is a valid json string in javascript?

Example of check if a string is a valid json string in javascript with step by step guide.

May 25th, 2022 · 1 min read

How to check if a string is a date in javascript?

Example of check if a string is a date in javascript with step by step guide.

May 24th, 2022 · 1 min read
© 2020–2022 Infinitbility
About
Link to $https://medium.com/infinitbilityLink to $https://www.facebook.com/InfinitbilityLink to $https://github.com/infinitbilityLink to $https://twitter.com/infinitbilityLink to $https://www.buymeacoffee.com/infinitbilityLink to $mailto:[email protected]