Example 1: Check String with includes()
Output
Enter a string: JavaScript is fun Enter a string that you want to check: fun The string contains fun
The includes() method is used with the if...else statement to check whether a string contains the characters of a specified string.
Note: The includes() method is case-sensitive. Hence, fun and Fun are different.
Example 2: Check String with indexOf()
Output
Enter a string: JavaScript is fun Enter a string that you want to check: fun The string contains fun
In the above program, the indexOf() method is used with the if...else statement to check if a string contains a substring.
The indexOf() method searches a string and returns the position of the first occurrence. When a substring cannot be found, it returns -1.
Note: The indexOf() method is case sensitive.