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 than includes(). 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.

TAGS
System Design Interview
CONTRIBUTOR
Design Gurus Team
-

GET YOUR FREE

Coding Questions Catalog

Design Gurus Newsletter - Latest from our Blog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
What is the difference between system design and coding?
What is the highest salary in Dell Technologies?
What are the strategies for coding interviews at consulting firms?
Is Meta a successful company?
Which country is best for tech jobs in 2024?
What is coding in computer networks?
Related Courses
Course image
Grokking the Coding Interview: Patterns for Coding Questions
Grokking the Coding Interview Patterns in Java, Python, JS, C++, C#, and Go. The most comprehensive course with 476 Lessons.
4.6
Discounted price for Your Region

$197

Course image
Grokking Modern AI Fundamentals
Master the fundamentals of AI today to lead the tech revolution of tomorrow.
3.9
Discounted price for Your Region

$78

Course image
Grokking Data Structures & Algorithms for Coding Interviews
Unlock Coding Interview Success: Dive Deep into Data Structures and Algorithms.
4
Discounted price for Your Region

$78

Image
One-Stop Portal For Tech Interviews.
Copyright © 2026 Design Gurus, LLC. All rights reserved.