JavaScript String Containment Check: Comprehensive Guide to indexOf and includes Methods

Nov 10, 2025 · Programming · 9 views · 7.8

Keywords: JavaScript | string check | indexOf method | includes method | substring search

Abstract: This article provides an in-depth exploration of two primary methods for checking string containment in JavaScript: indexOf and includes. Through detailed code examples and comparative analysis, it explains the syntax, parameters, return values, and usage scenarios of both methods, covering advanced techniques such as case sensitivity handling and search position configuration.

Fundamental Concepts of String Containment Checking

In JavaScript programming, checking whether a string contains another string is a common operational requirement. This operation is typically referred to as string containment checking or substring searching. Based on the Q&A data and reference articles, we can implement this functionality using various methods, with indexOf() and includes() being the two most commonly used approaches.

Detailed Explanation of indexOf Method

The indexOf() method is a long-standing string search method in JavaScript that returns the index position of the first occurrence of a specified substring within a string. If the substring is not found, it returns -1. This method works with all versions of JavaScript and offers excellent browser compatibility.

Basic syntax:

string.indexOf(searchValue, startPosition)

Where:

Based on the example from the Q&A, we can implement string containment checking as follows:

var str1 = "ABCDEFGHIJKLMNOP";
var str2 = "DEFG";
if(str1.indexOf(str2) != -1){
    console.log(str2 + " found");
}

This code first defines two string variables, then uses the indexOf() method to check whether str1 contains str2. If the return value is not equal to -1, it indicates that the substring was found, and the program outputs the corresponding notification message.

Detailed Explanation of includes Method

The includes() method was introduced in ECMAScript 6 (ES6) as a new method specifically designed for checking whether a string contains a specified substring. Unlike indexOf(), it directly returns a boolean value, making the code more intuitive and readable.

Basic syntax:

string.includes(searchValue, startPosition)

Parameter description:

Usage example:

let text = "Hello world, welcome to the universe.";
let result = text.includes("world");
console.log(result); // Output: true

Method Comparison and Selection

Both methods have their respective advantages:

Advantages of indexOf method:

Advantages of includes method:

In practical development, if you only need to know whether the string contains the substring without caring about the specific position, the includes() method is recommended; if you need to obtain the substring position or need to support older browsers, use the indexOf() method.

Case Sensitivity Handling

It's important to note that both methods are case-sensitive by default. This means "Hello".includes("hello") will return false.

If case-insensitive search is required, you can first convert the strings to uniform case:

const str = "This is my example string!";
const substr = "MY";
console.log(str.toLowerCase().includes(substr.toLowerCase())); // Output: true

Search Starting Position Configuration

Both methods support specifying the starting position for search, which can be very useful in certain specific scenarios:

let text = "Hello world, welcome to the universe.";

// Search starting from position 12
let result1 = text.includes("world", 12);
console.log(result1); // Output: false

// Search starting from position 0
let result2 = text.includes("world", 0);
console.log(result2); // Output: true

Other Related Methods

In addition to the two main methods mentioned above, JavaScript provides other string search methods:

Practical Application Scenarios

String containment checking has wide applications in web development:

By appropriately selecting and using these methods, development efficiency and code quality can be significantly improved.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.