In JavaScript, you can check whether a string contains a substring using the includes()
method, which returns a boolean value true
if the string contains the specified substring and false
otherwise.
Here’s an example:
const str = "Hello, world!"; const substr = "world"; if (str.includes(substr)) { console.log(`"${str}" contains "${substr}"`); } else { console.log(`"${str}" does not contain "${substr}"`); }
This will output: "Hello, world!" contains "world"
.