Javascript: Check if string contains a substring
Mar 21, 2024 • JavascriptIn Javascript, developers often encounter scenarios where they need to check whether a string contains a specific substring. The String.prototype.includes()
method offers a convenient solution.
Let’s dive into this method and explore how it simplifies the process of checking for substring presence within a string.
Understanding the String.prototype.includes()
Function
The String.prototype.includes()
method is a powerful addition to JavaScript’s string manipulation arsenal. Its syntax is straightforward:
const string = "foo";
const substring = "oo";
console.log(string.includes(substring));
// returns true
As demonstrated in the code snippet above, string.includes(substring)
returns true
if the substring is found within the string, and false
otherwise. This method performs a simple substring search, making it ideal for various string containment checks.
Advantages over traditional approaches
Prior to the introduction of String.prototype.includes()
, developers often resorted to alternative methods to check for substring containment. One common approach involved using the indexOf()
method:
const string = "foo";
const substring = "oo";
console.log(string.indexOf(substring) > -1);
// returns true
While effective, this approach has its drawbacks. It requires additional comparison (> -1
) (also !== -1
would work as well) to determine the presence of the substring, making the code slightly less readable and potentially prone to errors.
In contrast, String.prototype.includes()
offers a more intuitive and concise solution, enhancing code readability and maintainability.
Compatibility Considerations
String.prototype.includes()
was introduced in ECMAScript 6 (ES6). So, older browsers may not support this method. However, with the widespread adoption of ES6 features, compatibility concerns are not an issue with all modern browsers and Node.js supporting the function.
For environments where ES6 features are not supported, the use of indexOf()
is still an option.