What is includes() method in String - Javascript.

Nk Rafi
May 7, 2021

includes() is a method for the string that performs a case-sensitive search in a given string to find another string in it.

let myString = "Hello there I'm a string"
console.log(myString.includes(there));
#output:
true

The method takes two parameters includes(searchString, position). The first parameter is the string you want to find from the given string and the second one (which is optional) is the position within the string at which to begin searching for. (Default is 0)

The includes() method return Boolean true or false. It’s also a case-sensitive method. Example:

let myString = 'Hello World'
console.log(myString.includes('hello'));
#output:
false

Some example for clear understanding:

const myString2 = 'To be, or not to be, that is the question.'

console.log(myString2.includes('To be'));
console.log(myString2.includes('question'));
console.log(myString2.includes('nonexistent'));
console.log(myString2.includes('To be', 1);
console.log(myString2.includes('TO BE'));
console.log(myString2.includes(''));
#output:
true
true
false
false
false
true

--

--

Nk Rafi
0 Followers

Tech writer. Bridging complex tech and plain language. Passionate about innovation. Educating and inspiring readers to stay ahead.