javascript
  1. javascript-includes

JavaScript includes()

The includes() method in JavaScript is used to determine whether an array or a string includes a certain value. It returns a boolean value indicating whether the value is found or not. The method is case-sensitive for strings.

Syntax

The syntax for includes() method is as follows:

array.includes(valueToFind, fromIndex)

Here,

  • valueToFind is the value to be searched in the array.
  • fromIndex (optional) is the index at which to begin searching in the array. If not specified, it defaults to zero.

Example

const numbers = [1, 2, 3, 4, 5];

console.log(numbers.includes(2)); //true

console.log(numbers.includes(6)); //false

const message = "Hello, World!";

console.log(message.includes("Hello")); //true

console.log(message.includes("hello")); //false

Output

The output of the above code will be:

true
false
true
false

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Includes Example</title>
</head>
<body>

<script>
    // Array of numbers
    const numbers = [1, 2, 3, 4, 5];

    // Check if 2 is in the array
    document.write("<p> " + numbers.includes(2) + "</p>");

    // Check if 6 is in the array
    document.write("<p> " + numbers.includes(6) + "</p>");

    // String message
    const message = "Hello, World!";

    // Check if "Hello" is in the string
    document.write("<p>" + message.includes("Hello") + "</p>");

    // Check if "hello" is in the string
    document.write("<p> " + message.includes("hello") + "</p>");
</script>

</body>
</html>

Try Playground

Explanation

In the above example, we have an array of numbers and a string message. We have used the includes() method to check whether certain values are present in the array or the string.

In the first console.log() statement, the includes() method returns true because the value 2 is present in the array.

In the second console.log() statement, the includes() method returns false because the value 6 is not present in the array.

In the third console.log() statement, the includes() method returns true because the substring "Hello" is present in the string message.

In the fourth console.log() statement, the includes() method returns false because the substring "hello" is not present in the string message.

Use

The includes() method can be used to check whether a value exists in an array or a substring exists in a string. It is often used in conditional statements and loops.

Important Points

  • The includes() method is case-sensitive for strings.
  • The includes() method returns a boolean value indicating whether the value is found or not.
  • The includes() method can be used with arrays and strings.
  • The includes() method can take an optional fromIndex parameter to specify the starting index for searching in the array.

Summary

The includes() method is a useful and simple method for checking whether a value exists in an array or a substring exists in a string. It returns a boolean value indicating whether the value is found or not. The method is case-sensitive for strings and takes an optional fromIndex parameter to specify the starting index for searching in the array.

Published on: