check whether a string contains a substring in JavaScript

JavaScript
x
5
function checkSubstring(str, substr) {
return str.indexOf(substr) !== -1;
}
console.log(checkSubstring('Hello World', 'World')); // true
🤖 Code Explanation
The code defines a function that takes in two strings as arguments. The function checks if the second string is a substring of the first string and returns true or false. In the example, the function returns true because 'World' is a substring of 'Hello World'.

More problems solved in JavaScript




















