# How to check if a string contains a substring in JavaScript

By Atta Ur Rehman Shah (https://attacomsian.com/about). Published Apr 24, 2019, updated Oct 4, 2022. Topics: JavaScript.
Canonical URL: https://attacomsian.com/blog/ways-to-check-string-contains-substring-javascript

> A practical guide to learning about six different methods to check if a string contains a substring in JavaScript.

There are multiple ways to check if a string contains a substring in JavaScript. You can use either `String.includes()`, `String.indexOf()`, `String.search()`, `String.match()`, regular expressions, or 3rd-party library like Lodash.

## `String.includes()` Method

The `String.includes()`provides the most simple and popular way to check if a string contains a substring in modern JavaScript.

It was introduced in ES6 and works in all modern browsers except Internet Explorer. The `String.includes()` method returns `true` if the string contains the specified substring. Otherwise, it returns `false`.  

Here is an example:

```javascript
let str = 'MongoDB, Express, React, Node'

str.includes('MongoDB') //true
str.includes('Java') //false
str.includes('Node') //true
```

Note that `includes()` is case-sensitive and accepts an optional second parameter, an integer, which indicates the position where to start searching for.

```javascript
let str = 'MongoDB, Express, React, Node'

str.includes('express') // false (Due to case-sensitive)
str.includes('React', 5) // true
```

Read [this article](https://attacomsian.com/blog/javascript-includes-method) to learn more about the `String.includes()` method in JavaScript.

## `String.indexOf()` Method

The `String.indexOf()` method returns the index of the first occurrence of the substring. If the string does not contain the given substring, it returns `-1`. 

The `indexOf()` method is case-sensitive and accepts two parameters. The first parameter is the substring to search for, and the second optional parameter is the index to start the search from (the default index is `0`).  

```javascript
let str = 'MongoDB, Express, React, Node'

str.indexOf('MongoDB') !== -1 // true
str.indexOf('Java') !== -1 // false
str.indexOf('Node', 5) !== -1 // true
```

To learn more about the `String.indexOf()` method, read [this guide](https://attacomsian.com/blog/javascript-string-indexof-method).

## `String.search()` Method

The `String.search()` method searches the position of the substring in a string and returns the position of the match. 

The search value can be a string or a [regular expression](https://attacomsian.com/blog/javascript-regular-expressions). It returns `-1` if no match is found.

```javascript
let str = 'MongoDB, Express, React, Node'

str.search('MongoDB') !== -1 // true
str.search('Java') !== -1 // false
str.search(/node/i) !== -1 // true where i is the case insensitive modifier
```

## `String.match()` Method

The `String.match()` method accepts a [regular expression](https://attacomsian.com/blog/javascript-regular-expressions) as a parameter and searches the string for a match. If it finds the matches, it returns an object, and `null` if no match is found.

```javascript
let str = 'MongoDB, Express, React, Node'

str.match(/MongoDB/)
// ['MongoDB', index: 0, input: 'MongoDB, Express, React, Node', groups: undefined]

str.match(/Java/) // null
str.match(/MongoDB/g) !== null // true
str.match(/Java/g) !== null // false
str.match(/node/i) !== null // true where i is the case insensitive modifier
```

## `RegExp.test()` Method

The `RegExp.test()` method executes a search for a match between a regular expression and a specified string. It returns `true` if it finds a match. Otherwise, it returns `false`.

```javascript
let str = 'MongoDB, Express, React, Node'

const exp = new RegExp('MongoDB', 'g')
exp.test(str) // true

/Java/g.test(str) // false
/node/i.test(str) // true where i is the case insensitive modifier 
```

## Lodash `_.includes()` Method

[Lodash](https://lodash.com/) is a third-party library that provides `_.includes()` method to check the presence of a substring in a string. This method returns `true` if a match is found, otherwise, `false`.

```javascript
let str = 'MongoDB, Express, React, Node'

_.includes(str, 'MongoDB') // true
_.includes(str, 'Java') // false
_.includes(str, 'Node') // true
```
