Skip to content
  • (+91) 9409548155
  • support@appdividend.com
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Menu
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
JavaScript

JavaScript String includes(): Check If a String Contains a Substring

  • 12 Sep, 2025
  • Com 0
JavaScript Array includes() Method

JavaScript String includes() is a built-in function that checks for the presence of a substring (or sequence of characters) within a string and returns true if a string contains a substring and false otherwise. It is a modern approach for string.indexOf() method.

Check If a String Contains a Substring in JavaScript using string.includes()

const str = "Nvidia Company!";

console.log(str.includes("Nvidia"));

// Output: true (found as a contiguous sequence)

console.log(str.includes("Apple"));

// Output: false

In the above code, the substring “Nvidia” exists in the str, so it returns true, but str does not contain “Apple”, which is why it returns false in that case.

Syntax

string.includes(search_string, position)

Parameters

Argument Description
search_string (required)

It is the substring to search for within the calling string. If it is not a string, it is coerced into one.

position (optional, default 0) It represents the index at which to start the search.

If you omit it, the search begins from the start of the string (index 0).

Checking case-sensitiveness

The String includes() method is case-sensitive; even if the strings are the same, but the cases differ, it returns false.

Case-sensitiveness in string.includes() method

const str = "Nvidia Company!";

console.log(str.includes("nvidia"));

// Output: false (case-sensitive)

If you want to ignore case sensitivity, convert the input string to lowercase using string.toLowerCase() method.

const str = "Nvidia is the biggest company!";

lowerStr = str.toLowerCase();

console.log(lowerStr.includes("nvidia"));

// Output: true

Now, each character of the input string is converted to lowercase, and the substring “nvidia” matches with the input string’s “nvidia”.

With the “Position” argument

The “position” shifts the search start. The character index of a string starts from 0. By default, it searches from the start, but you can skip it based on your requirements.

With the Position argument in string.includes() method

Simple example

Let’s say we have an input string called “Amazon” and we want to search for the “zon” substring starting from the beginning of “Amazon” and from the 4th position. Let’s see the outputs.

const main_str = "Amazon";

console.log(main_str.includes("zon"));

// Output: true

console.log(main_str.includes("zon", 4));

// Output: false

In the first case, we start from the 0th index, so it finds the “zon” substring and returns true.

In the second case, our starting point is the 4th position, and it has an index of 4 (starting from 0). 

Therefore, it won’t find the “zon” substring because at the 4th index, the character is “o” and not “z”. So, it returns false. 

Complex example

const str = "Nvidia is the biggest company!";

console.log(str.includes("Nvidia", 0));

// Output: true (starts from beginning)

console.log(str.includes("Nvidia", 1));

// Output: false (It starts from 0th index but we provided 1st index)

console.log(str.includes("company", 23));

// Output: false (It starts from 22nd index but we provided 23rd index)

console.log(str.includes("biggest", 13));

// Output: true (starts after 13th index)

In this code, we saw multiple scenarios. In the first scenario, we started searching for the “Nvidia” substring from the 0th index, and since it was found, it returned true.

However, in the second scenario, we started finding “Nvidia” from the 1st index, but it would find “vidia” instead of “Nvidia”, so it returned false as output. The start position matters the most in this case.

The same applies to the third scenario, where we started finding the “company” substring from position 23, which returned false because it started from position 22.

In the final use case, we were searching for the “biggest” substring from the 13th position. Since the string starts at the 14th index, it returns true because it finds the match when it reaches the 14th index.

Empty string behavior

If the substring you are to find is an empty string, the includes() method returns true for any input string because the input string always contains an empty string.

const str = "Nvidia is the biggest company!";

const substr = ""

console.log(str.includes(substr));

// Output: true

Position Out of Bounds

If the string has 6 characters and your starting index for searching is 8th, the string includes() method returns false because the position is out of bounds for the input string.

const str = "Krunal";

const substr = "l"

console.log(str.includes(substr, 8));

// Output: false

That’s all!

Post Views: 3
Share on:
Krunal Lathiya

With a career spanning over eight years in the field of Computer Science, Krunal’s expertise is rooted in a solid foundation of hands-on experience, complemented by a continuous pursuit of knowledge.

How to Add Property to an Object in JavaScript
How to Find the Length of an Object in JavaScript

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Address: TwinStar, South Block – 1202, 150 Ft Ring Road, Nr. Nana Mauva Circle, Rajkot(360005), Gujarat, India

Call: (+91) 9409548155

Email: support@appdividend.com

Online Platform

  • Pricing
  • Instructors
  • FAQ
  • Refund Policy
  • Support

Links

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of services

Tutorials

  • Angular
  • React
  • Python
  • Laravel
  • Javascript
Copyright @2024 AppDividend. All Rights Reserved
Appdividend