OneCompiler

[Javascript] How to know if a character is present in a given string?

I want to check if a character is present in a given string, How to do that in Javascript?

1 Answer

4 years ago by

You can use either indexOf() or includes to check if a character is present in a given string.

let str = 'this is a test message';

console.log(str.includes('w'));
console.log(str.includes('m'));

console.log(str.indexOf('w') > 0);
console.log(str.indexOf('w') > 0);

Run here https://onecompiler.com/javascript/3xnrukbnz

4 years ago by Meera