Question: How to check whether a string contains a substring in JavaScript
Answer:
First: Using 'includes()'.
const string = "RustcodeWeb"; const substring1 = "code"; const substring2 = "Web"; console.log(string.includes(substring1)); console.log(string.includes(substring2));
Output:
true true
Second: Using 'indexof'.
const string = "RustcodeWeb"; const substring1 = "code"; const substring2 = "Web"; console.log(string.indexOf(substring1) !== -1); console.log(string.indexOf(substring2) !== -1);
Output:
true true
We try to provide you the best content, if there is any mistake in this article or there is any mistake in code, then let us know.
Comments
Post a Comment