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
- Create a variable and assign some value to it
- Use the
includes()
method to check string contains sub string or not - Console output substring contain or not
// Create a variable and assign some value on it
const slogan = "We have the ability to build infinite way for us.";
// Use the `includes()` method to check string contain sub string or not
// Console output substring contain or not
if(slogan.includes("infinite")){
console.log("SubString contain");
} else {
console.log("SubString not contain");
}
When running the above program it should console 'SubString contain'
in the log. let’s check the output.
I hope it’s help you, All the best 👍.