How to check whether a string contains a substring in JavaScript?
How to Check Whether a String Contains a Substring in JavaScript
In JavaScript, there are several methods to check if a string contains a substring. Here are the most commonly used methods:
1. Using includes()
Method
The includes()
method determines whether a string contains the characters of a specified string, returning true
or false
as appropriate.
Syntax:
string.includes(substring, start);
substring
: The string to search for.start
(optional): The position in the string at which to start the search. Default is 0.
Example:
let text = "Hello, world!"; let result = text.includes("world"); console.log(result); // Output: true
2. Using indexOf()
Method
The indexOf()
method returns the position of the first occurrence of a specified value in a string. It returns -1
if the value is not found.
Syntax:
string.indexOf(substring, start);
substring
: The string to search for.start
(optional): The position in the string at which to start the search. Default is 0.
Example:
let text = "Hello, world!"; let result = text.indexOf("world") !== -1; console.log(result); // Output: true
3. Using search()
Method
The search()
method searches a string for a specified value or regular expression and returns the position of the match. It returns -1
if no match is found.
Syntax:
string.search(regexp);
regexp
: The regular expression to search for.
Example:
let text = "Hello, world!"; let result = text.search("world") !== -1; console.log(result); // Output: true
4. Using Regular Expressions with test()
Method
The test()
method executes a search for a match between a regular expression and a specified string. It returns true
or false
.
Syntax:
regexp.test(string);
Example:
let text = "Hello, world!"; let pattern = /world/; let result = pattern.test(text); console.log(result); // Output: true
Comparison of Methods
includes()
: Simple and modern method. Best for straightforward substring checks. Introduced in ES6.indexOf()
: Older method, works similarly but less readable thanincludes()
. Useful for more complex checks where the position is needed.search()
: Useful for regex searches, but less commonly used for simple substring checks.test()
: Best for complex pattern matching using regular expressions.
Example Comparison
Here’s a quick comparison of the methods to check if a string contains the substring "world":
let text = "Hello, world!"; // Using includes() console.log(text.includes("world")); // Output: true // Using indexOf() console.log(text.indexOf("world") !== -1); // Output: true // Using search() console.log(text.search("world") !== -1); // Output: true // Using test() with regular expression let pattern = /world/; console.log(pattern.test(text)); // Output: true
Summary
To check whether a string contains a substring in JavaScript, you can use methods like includes()
, indexOf()
, search()
, or test()
. The includes()
method is the most straightforward and modern way to perform this check, while the other methods offer additional flexibility for more complex searches.
For more comprehensive tutorials and practical examples of JavaScript and other programming concepts, consider exploring Grokking the Coding Interview on DesignGurus.io, which provides in-depth coverage of essential coding techniques and interview preparation.
GET YOUR FREE
Coding Questions Catalog