JavaScript Program to Check Whether a String Contains a Substring

bookmark

// program to check if a string contains a substring

// take input
const str = prompt('Enter a string:');
const checkString = prompt('Enter a string that you want to check:');

// check if string contains a substring
if(str.includes(checkString)) {
    console.log(`The string contains ${checkString}`);
} else {
    console.log(`The string does not contain ${checkString}`);
}

 

 

Output

Enter a string: JavaScript is fun
Enter a string that you want to check: fun
The string contains fun