Hi Friends 👋,
Welcome To Infinitbility! ❤️
Today, I’m going to show How do I check if a string contains a specific character in Javascript, here I will use the javascript string includes()
method to check if a string contains a specific character or not.
What is a includes()
method?
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 do you check if a string contains a specific character in JavaScript?
Let’s understand how we can do it
- Create a string variable
- Use the
includes()
method and pass specific character - Console the which string contains a specific character
// Create a string variable
const str = 'Hello world';
const char = 'e';
// Use `includes()` method and pass specific character
// Console the which string contains a specific character
console.log(str.includes(char)); // 👉️ true
if (str.includes(char)) {
// 👉️ string contains the character
}
The above program is the simplest way to check string contains a specific character or not. let’s check the output.
I hope it’s help you, All the best 👍.