Skip to content

WebDevHubs

  • Home
  • HTML
  • CSS
  • JavaScript
  • Web Technologies
  • Web Templates
  • Toggle search form

JavaScript Array includes() Method

Posted on June 16, 2025June 16, 2025 By Admin No Comments on JavaScript Array includes() Method

JavaScript Array includes() method determines whether an array contains a specific value or not. It returns a boolean value (true or false) based on whether the specified value exists in the array.

JavaScript Array includes() Method

Pre-requisites to Learn

  • JavaScript Array

Syntax

arr.includes(valueToFind, fromIndex);

Parameters

ParameterDescription
valueToFind (Required)The value to search for in the array.
fromIndex (Optional)The index to start the search from. Defaults to 0. If negative, it counts from the end of the array.

Return Value

The includes() method returns true if the specified value is found in the array, otherwise returns false.

Notes:

  1. The search is case-sensitive for strings.
  2. Unlike indexOf(), the includes() method can correctly detect NaN as a valid value in the array.
  3. It does not modify the original array.

Examples of JavaScript Array includes() Method

Example 1: Checking whether specific fruits are present in the array.

let fruits = ['apple', 'banana', 'cherry'];

console.log(fruits.includes('banana')); // Output: true
console.log(fruits.includes('grape'));  // Output: false

Example 2: The includes() method is case-sensitive i.e. the 'apple' and 'Apple' are treated as different values.

let fruits = ['Apple', 'Banana', 'Cherry'];

console.log(fruits.includes('apple')); // Output: false
console.log(fruits.includes('Apple')); // Output: true

Example 3: The fromIndex parameter specifies where the search should start. Negative values count backward from the end of the array.

let numbers = [10, 20, 30, 40, 50];

console.log(numbers.includes(30, 2));  // Output: true
console.log(numbers.includes(30, 3));  // Output: false
console.log(numbers.includes(30, -3)); // Output: true

Example 4: Handling NaN Values – Unlike indexOf(), which cannot detect NaN, the includes() method correctly identifies its presence.

let values = [1, 2, NaN, 4];

console.log(values.includes(NaN)); // Output: true

Example 5: In sparse arrays, empty slots are treated as undefined by the includes() method.

let sparseArray = [1, , 3];

console.log(sparseArray.includes(undefined)); // Output: true

Supported Browsers

BrowserSupport
Chrome47+
Firefox43+
Safari9+
Edge14+
Opera34+
Internet ExplorerNot supported

Comparison with Other Methods

MethodPurpose
Array.includes() MethodChecks if an array contains a value and handles NaN correctly. Returns true or false.
Array.indexOf() MethodReturns the index of a value in an array or -1 if not found. Does not handle NaN.
Array.some() MethodChecks if at least one element satisfies a given condition.
JavaScript, Web Technologies Tags:JavaScript-Array, JavaScript-Array-Method, JavaScript-Method

Post navigation

Previous Post: JavaScript Array keys() Method
Next Post: JavaScript Array forEach() Method

Leave a Reply Cancel reply

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

Archives

  • June 2025
  • May 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024

Categories

  • CSS
  • HTML
  • JavaScript
  • Lodash
  • PHP
  • Python
  • Web Technologies
  • Web Templates

Recent Posts

  • JavaScript Array isArray() Method
  • JavaScript Array forEach() Method
  • JavaScript Array includes() Method
  • JavaScript Array keys() Method
  • JavaScript Array lastIndexOf() Method

Recent Comments

No comments to show.

Copyright © 2025 WebDevHubs.

Powered by PressBook Green WordPress theme