DEV Community

loading...

Answer: Fastest way to check a string contain another substring in JavaScript?

mhsohag11 profile image mhsohag11 ・1 min read

In ES6, the includes() method is used to determine whether one string may be found within another string, returning true or false as appropriate.

var str = 'To be, or not to be, that is the question.';

console.log(str.includes('To be'));       // true
console.log(str.includes('question'));    // true
console.log(str.includes('nonexistent')); // false

Here is jsperf

Discussion (0)

pic
Editor guide