You can use the includes() method to check if a string contains a substring in JavaScript. The method returns a boolean value indicating whether the string includes the substring. Here’s an example:

let str = "Hello, world!";
let substring = "world";
console.log(str.includes(substring)); // true

Alternatively, you can use the indexOf() method which returns the index of the first occurrence of the substring in the string, or -1 if the substring is not found. Here’s an example:

let str = "Hello, world!";
let substring = "world";
console.log(str.indexOf(substring) !== -1); // true

Or you can use a regex to check if the substring exist or not.

let str = "Hello, world!";
let substring = "world";
console.log(new RegExp(substring).test(str)); // true

Also Read:

Categorized in: