menu

JavaScript/Typescript - 16 Topics

DEEP DIVE INTO

JavaScript/Typescript

Topic:includes() method

menu

The includes() method in JavaScript is a built-in method for arrays and strings, and it is used to check if an array contains a specific element or if a string contains a specified substring. It returns a boolean value (true or false) based on whether the element or substring is found. Let's take a deep dive into the includes() method and explore how it works for both arrays and strings.

Array includes() Method:

Basic Syntax (Arrays):

The basic syntax of the includes() method for arrays is as follows:

javascriptarray.includes(elementToFind[, fromIndex])
  • array: The array in which you want to search for the element.

  • elementToFind: The element you want to check if it exists in the array.

  • fromIndex (optional): An integer representing the index to start the search from. If omitted, the default is 0.

Example Usage (Arrays):

javascriptconst fruits = ['apple', 'banana', 'cherry', 'date'];
const includesBanana = fruits.includes('banana');
console.log(includesBanana); // Output: true

In this example, we use the includes() method to check if the fruits array contains the element "banana." Since it does, the method returns true.

Using the fromIndex Parameter (Arrays):

The fromIndex parameter allows you to specify the index at which to start the search. If the element is found at or after the specified index, true is returned; otherwise, false is returned.

javascriptconst numbers = [1, 2, 3, 4, 5];
const includesThree = numbers.includes(3, 2);
console.log(includesThree); // Output: true

In this example, we specify fromIndex as 2, indicating that we want to start the search from the index 2 (which corresponds to the number 3). As a result, the method returns true.

String includes() Method:

Basic Syntax (Strings):

The basic syntax of the includes() method for strings is as follows:

javascriptstring.includes(substring[, position])
  • string: The string in which you want to search for the substring.

  • substring: The .you want to check if it exists in the string.

  • position (optional): An integer representing the position within the string from which to start the search. If omitted, the default is 0.

Example Usage (Strings):

javascriptconst text = 'Hello, world!';
const includesWorld = text.includes('world');
console.log(includesWorld); // Output: true

In this example, we use the includes() method to check if the text string contains the substring "world." Since it does, the method returns true.

Using the position Parameter (Strings):

The position parameter allows you to specify the position within the string from which to start the search. If the substring is found at or after the specified position, true is returned; otherwise, false is returned.

javascriptconst text = 'Hello, world!';
const includesWorld = text.includes('world', 7);
console.log(includesWorld); // Output: true

In this example, we specify position as 7, indicating that we want to start the search from the 8th character (0-based index) of the string. As a result, the method returns true because "world" starts at that position.

Use Cases:

Theincludes() method is commonly used for various purposes, including:

1. Array searching: Checking if an array contains a specific value or element.

2. Substring checking: Verifying if a string contains a particular substring.

3. Filtering: Filtering arrays based on the presence of certain elements.

4. Conditional logic: Making decisions in code based on the presence of specific values.

5. Testing for inclusion: Verifying whether an item is part of a set or collection.

In summary, the includes() method in JavaScript is a versatile tool for checking if an array contains a specific element or if a string contains a specified substring. It's widely used for conditional logic, filtering, and searching in JavaScript applications.

1280 x 720 px