Check if a String contains a Substring in JavaScript
Published: 2024-03-06 13:31:53

Title :[ Check if a String contains a Substring in JavaScript](https://askyt.info/check-if-a-string-contains-a-substring-in-javascript/)

### 1. Check if a String contains a Substring with String.includes():

```javascript
const str1 = "Hello, world!";
const substring1 = "world";

const containsSubstring1 = str1.includes(substring1);
console.log(containsSubstring1); // Output: true
```

Explanation:
- We use the `includes()` method on the string `str1` to check if it contains the substring `"world"`.
- Since "world" is a part of "Hello, world!", `includes()` returns `true`.

### 2. Check if String Doesn't contain a Substring in [JavaScript](https://askyt.info/category/javascript/):

```javascript
const str2 = "Hello, world!";
const substring2 = "foo";

const doesNotContainSubstring2 = !str2.includes(substring2);
console.log(doesNotContainSubstring2); // Output: true
```

Explanation:
- We use the `includes()` method to check if the string `str2` contains the substring `"foo"`.
- Since "foo" is not a part of "Hello, world!", `includes()` returns `false`.
- Using the logical NOT (`!`) operator, we negate the result, which gives us `true`.

### 3. Check if a String contains a Substring with String.indexOf():

```javascript
const str3 = "Hello, world!";
const substring3 = "world";

const containsSubstring3 = str3.indexOf(substring3) !== -1;
console.log(containsSubstring3); // Output: true
```

Explanation:
- We use the `indexOf()` method on the string `str3` to check if it contains the substring `"world"`.
- `indexOf()` returns the index of the substring in the string if found, otherwise `-1`.
- Since the substring "world" is found in "Hello, world!" at index position 7, `indexOf()` doesn't return `-1`, so the condition evaluates to `true`.

### 4. Check if String Doesn't contain a Substring using indexOf():

```javascript
const str4 = "Hello, world!";
const substring4 = "foo";

const doesNotContainSubstring4 = str4.indexOf(substring4) === -1;
console.log(doesNotContainSubstring4); // Output: true
```

Explanation:
- We use the `indexOf()` method to get the index of the substring `"foo"` in the string `str4`.
- Since "foo" is not a part of "Hello, world!", `indexOf()` returns `-1`.
- We check if the returned value is equal to `-1` to determine if the string doesn't contain the substring, which evaluates to `true`.

### 5. Using includes() and indexOf() with case sensitivity:

```javascript
const str5 = "Hello, world!";
const substring5 = "WORLD";

const containsSubstringCaseSensitive = str5.includes(substring5);
console.log(containsSubstringCaseSensitive); // Output: false

const containsSubstringIgnoreCase = str5.toLowerCase().includes(substring5.toLowerCase());
console.log(containsSubstringIgnoreCase); // Output: true
```

Explanation:
- By default, both `includes()` and `indexOf()` are case-sensitive.
- In the first example, `"WORLD"` is not found in `"Hello, world!"` because of the different case, so `includes()` returns `false`.
- In the second example, we convert both the string and the substring to lowercase before checking, so `"world"` is found in `"hello, world!"`, and `includes()` returns `true`.