The Wayback Machine - https://web.archive.org/web/20240924171747/https://www.geeksforgeeks.org/javascript-string-includes-method/
Open In App

JavaScript String includes() Method

Last Updated : 10 Sep, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The includes() method returns true if a string contains a specified string. Otherwise, it returns false.

Note: The includes() method is case sensitive i.e. it will treat the Uppercase Characters and Lowercase Characters differently.

Syntax:

string.includes(searchvalue, start)

Parameters:

  • search value: It is the string in which the search will take place.
  • start: This is the position from where the search will be processed 
    (although this parameter is not necessary if this is not mentioned the search will begin from the start of the string).

Return Value:

Returns either a Boolean true indicating the presence or it returns a false indicating the absence.

Example 1: The code checks if the string “Geeks” is present in the string “Welcome to GeeksforGeeks.”. It then logs the result, which is true, since “Geeks” is indeed present in the string.

javascript
let str = "Welcome to GeeksforGeeks.";
let check = str.includes("Geeks");
console.log(check);

Output
true

Example 2: Here, the second parameter is not defined, so the search will take place from the starting index. But as this method is case sensitive it will treat the two strings differently, hence returning a boolean false. 

javascript
let str = "Welcome to GeeksforGeeks.";
let check = str.includes("geeks");
console.log(check);

Output
false

Example 3: The code checks if the character “o” is present in the string “Welcome to GeeksforGeeks.” starting from index 17. It then logs the result, which is false, since “o” is not present in the substring starting from index 17.

javascript
let str = "Welcome to GeeksforGeeks.";
let check = str.includes("o", 17);
console.log(check);

Output
true

Example 4: If the computed index (starting index) i.e. the position from which the search will begin is less than 0, the entire array will be searched. 

javascript
let str = "Welcome to GeeksforGeeks.";
let check = str.includes("o", -2);
console.log(check);

Output
true

Supported Browsers:

JavaScript String includes() Method- FAQs

What does the includes() method do in JavaScript?

The includes() method in JavaScript checks if a string contains a specified substring. It returns true if the substring is found and false otherwise. This method is case-sensitive, meaning it differentiates between uppercase and lowercase characters.

How does case sensitivity affect the includes() method?

The includes() method is case-sensitive, which means it treats uppercase and lowercase characters as distinct. For example, searching for “Geeks” in a string will not match “geeks”.

What happens if the start parameter is not specified in the includes() method?

If the start parameter is not specified, the includes() method will begin searching from the beginning of the string.

How does the includes() method behave with a negative start index?

If the start index is negative, the includes() method treats it as 0, meaning it will search the entire string from the beginning.

Can the includes() method be used to search for substrings at specific positions?

Yes, the includes() method can be used to search for substrings starting at a specific position by using the start parameter.


Previous Article
Next Article

Similar Reads

String includes() Method in JavaScript
JavaScript str.includes() function is used to check if the argument string is present in the given string or not. This function is case-sensitive. The syntax of this function is as follows: str.includes(searchString, position) Arguments The first argument to this function searchString is the string to be searched in the given string. The second arg
2 min read
TypeScript String includes() Method
The includes() method in TypeScript, inherited from JavaScript, is used to determine whether a given string contains a specified substring. This method returns a boolean value: true if the substring is found and false otherwise. SyntaxLet's start by understanding the syntax of the includes() method. string.includes(searchValue, fromIndex?: number):
2 min read
JavaScript typedArray.includes() Method
The typedArray.includes() is an inbuilt function in JavaScript which is used to check whether a particular element is included by the given typedArray or not and accordingly it returns true and false. Syntax: typedarray.includes(Element, Index); Parameters: It accepts two parameter which are specified below- Element: It is the element which are bei
2 min read
What is includes() Method in JavaScript ?
In JavaScript, the includes() method is used to determine whether a string contains another string or a specified value. It returns true if the string contains the specified value, and false otherwise. Syntax:string.includes(searchValue, startIndex)string: The string in which to search for the specified value.searchValue: The value to search for wi
1 min read
JavaScript TypedArray.prototype.includes() Method
In JavaScript the TypedArray.prototype.includes() method is used to check that the given typed array contains a specific given element or not, if it contains the element it returns true otherwise false. SyntaxtypedArray.includes(searchElement)ORtypedArray.includes(searchElement, fromIndex)ParameterssearchElement: The value to search for. fromIndex:
1 min read
JavaScript Array includes() Method
The includes() method returns true if an array contains a specified value. Conversely, it returns false if the value is not found. This method simplifies checking for the presence of an element within an array, providing a straightforward boolean result. Syntaxarray.includes(searchElement, start);ParameterssearchElement: This parameter holds the el
3 min read
How to check whether an array includes a particular value or not in JavaScript ?
In this article, we will discuss the construction of an array followed by checking whether any particular value which is required by the user is included (or present) in the array or not. But let us first see how we could create an array in JavaScript using the following syntax- Syntax: The following syntax would be helpful for any user for the cre
3 min read
How do I check if an Array includes a value in JavaScript?
We are going to learn how to check if a value is present in an array or not. We will need the array we want to search through and the target element we are looking for. JavaScript arrays serve as convenient containers for holding lists of elements, accessible through a single variable. Once we have a target element we can perform various search alg
4 min read
How to check if an array includes an object in JavaScript ?
Check if an array includes an object in JavaScript, which refers to determining whether a specific object is present within an array. Since objects are compared by reference, various methods are used to identify if an object with matching properties exists in the array. These are the following approaches by using these we can check if an array incl
3 min read
Ember.js Ember.NativeArray includes() Method
Ember.js is an open-source JavaScript framework used for developing large client-side web applications which is based on Model-View-Controller (MVC) architecture. Ember.js is one of the most widely used front-end application frameworks. It is made to speed up development and increase productivity. Currently, it is utilized by a large number of webs
4 min read
Ember.js MutableArray includes() Method
Ember.js is an open-source JavaScript framework used for developing large client-side web applications which is based on Model-View-Controller (MVC) architecture. Ember.js is one of the most widely used front-end application frameworks. It is made to speed up development and increase productivity. Currently, it is utilized by a large number of webs
4 min read
Ember.js EmberArray includes() Method
Ember.js is an open-source JavaScript framework used for developing large client-side web applications which is based on Model-View-Controller (MVC) architecture. Ember.js is one of the most widely used front-end application frameworks. It is made to speed up development and increase productivity. Currently, it is utilized by a large number of webs
4 min read
Ember.js ArrayProxy includes() Method
Ember.js is an open-source JavaScript framework used for developing large client-side web applications which is based on Model-View-Controller (MVC) architecture. Ember.js is one of the most widely used front-end application frameworks. It is made to speed up development and increase productivity. Currently, it is utilized by a large number of webs
3 min read
Node.js Buffer.includes() Method
Buffer is a temporary memory storage which stores the data when it is being moved from one place to another. It is like an array of integers. The Buffer.includes() method checks whether the provided value is present or included in the buffer or not. Syntax: buffer.includes( value, byteOffset, encoding );Parameters: This method accept three paramete
2 min read
TypeScript Array includes() Method
The TypeScript Array.includes() method checks if an array contains a specified element, returning true if found and false otherwise. It performs a strict equality comparison (===), works with all data types, and correctly identifies NaN in arrays. Syntaxarray.includes(searchElement[, fromIndex])Parameters:searchElement: The value to search for with
2 min read
Lodash _.includes() Method
Lodash _.includes() method is used to find whether the value is in the collection or not. If the collection is a string, it will be tested for a value sub-string, otherwise SameValueZero() method is used for equality comparisons. If the index is given and is negative, the value is tested from the end indexes of the collection as the offset. Syntax:
3 min read
What does Bootstrap Package includes ?
It contains HTML and CSS-based design templates for typography, forms, buttons, and other interface components, as well as optional JavaScript extensions. A website has many different elements such as headings, lists, tables, images, buttons, forms, etc. All these fundamental HTML elements have been styled and enhanced with extensible classes. The
3 min read
How to Check if an Array Includes an Object in TypeScript ?
In TypeScript, checking if an array includes an object consists of comparing the object's properties within the array elements. We can compare and check this using three different approaches some method, find method, and includes method. There are several ways to check if an array includes an object in TypeScript which are as follows: Table of Cont
3 min read
What is includes in Pug View Engine ?
In Pug, "includes" are a way to modularize and reuse code by importing separate Pug files into a main Pug file. This helps in organizing code more efficiently and reduces duplication of code by allowing you to include common sections or components across multiple pages easily. In simple words, Includes allows you to insert the contents of one Pug f
3 min read
AngularJS Includes
AngularJS includes, also called as ng-include directive, allows you to insert external HTML content dynamically into an AngularJS application. This capability is particularly useful for modularizing applications, improving code organization, and enhancing code reusability. Syntax: <element ng-include=" "> content... <element>Key Feature
3 min read
Javascript Program to Check if a string can be formed from another string by at most X circular clockwise shifts
Given an integer X and two strings S1 and S2, the task is to check that string S1 can be converted to the string S2 by shifting characters circular clockwise atmost X times. Input: S1 = "abcd", S2 = "dddd", X = 3 Output: Yes Explanation: Given string S1 can be converted to string S2 as- Character "a" - Shift 3 times - "d" Character "b" - Shift 2 ti
3 min read
Javascript Program to Check if a string can be obtained by rotating another string d places
Given two strings str1 and str2 and an integer d, the task is to check whether str2 can be obtained by rotating str1 by d places (either to the left or to the right). Examples: Input: str1 = "abcdefg", str2 = "cdefgab", d = 2 Output: Yes Rotate str1 2 places to the left. Input: str1 = "abcdefg", str2 = "cdfdawb", d = 6 Output: No Approach: An appro
4 min read
Javascript Program to Check if a string can be obtained by rotating another string 2 places
Given two strings, the task is to find if a string can be obtained by rotating another string two places. Examples: Input: string1 = "amazon", string2 = "azonam" Output: Yes // rotated anti-clockwiseInput: string1 = "amazon", string2 = "onamaz" Output: Yes // rotated clockwise Asked in: Amazon Interview Recommended: Please solve it on “PRACTICE ” f
2 min read
JavaScript Program to Return Original String of Compressed String
The compressed string follows a format where each character is followed by its count. we need to print a string having the same number of characters present in the compressed string. Below are the approaches to return original string of compressed string using Example: Input: compressedString= "a3b2c1"Output: originalString="aaabbc"These are the fo
2 min read
Smallest Window in a String Containing all the Characters of Another String using JavaScript
Given two strings, "str" (the larger string) and "pattern" (the smaller string), the task is to find the smallest window in "str" that contains all the characters of "pattern" in any order using JavaScript. Examples: Input: str = "ADOBECODEBANC"pattern = "ABC"Output: BANCInput: str = "this is a test string"pattern = "tist";Output: t striTable of Co
3 min read
JavaScript Insert a string at position X of another string
Given two strings, the task is to insert one string in another at a specified position using JavaScript. We're going to discuss a few methods, these are: Methods to Insert String at a Certain IndexTable of Content Method 1: Using JavaScript String slice() and Array join() MethodsMethod 2: JavaScript String substr() Method Method 3: Using JavaScript
4 min read
How to check a given string is an anagram of another string in JavaScript ?
In this article, we will learn how to check a given string is an anagram of another string in JavaScript. Before that, we should learn what is an anagram. An anagram is a word or sentence, which usually contains all the original letters exactly once, in order to arrange the letters of a different term or phrase. Some of the examples are given below
3 min read
How to check if a string "StartsWith" another string in JavaScript ?
In JavaScript, the 'startsWith()' method is a built-in method that allows one to check whether a string starts with a specified substring. Syntax: It takes a single argument, which is the substring we want to check whether the string starts with. string.startsWith(searchString[, position])Approach 1: Using startWith methodThe 'searchString' paramet
2 min read
Check if a Given String is Binary String or Not in JavaScript
Binary strings are sequences of characters containing only the digits 0 and 1. Other than that no number can be considered as Binary Number. We are going to check whether the given string is Binary or not by checking it's every character present in the string. Example: Input: "101010"Output: True, binary stringInput: "110211"Output: Falee,Explanati
4 min read
Difference between String.slice and String.substring in JavaScript
These 2 functions are quite similar in their Syntax But are different in some cases. Let's see the difference between them. JavaScript slice() Method:This method selects the part of a string and returns the selected part as a new string. Start and end parameters are used to specify the extracted part. The first character starts with index 0. Syntax
3 min read