Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

JavaScript Dictionary

  1. Home
  2. JavaScript Dictionary
  3. Array .includes() / indexOf() / lastIndexOf()

Array .includes() / indexOf() / lastIndexOf()

Since: ES2016(ECMAScript 2016)

Methods for checking whether an array contains a specific value or finding the position of a value. includes() returns a boolean, while indexOf() returns an index.

Syntax

var result = array.includes(searchValue, startIndex);

var index = array.indexOf(searchValue, startIndex);

var index = array.lastIndexOf(searchValue, startIndex);

Methods

MethodDescription
includes(value, startIndex)Returns true if the array contains the specified value, or false if it does not. The start index is optional.
indexOf(value, startIndex)Returns the index of the first occurrence of the specified value. Returns -1 if not found.
lastIndexOf(value, startIndex)Returns the index of the last occurrence of the specified value. Returns -1 if not found.

Sample Code

var members = ["item_a", "item_b", "item_c", "item_b", "item_d"];

console.log(members.includes("item_b")); // Outputs: true
console.log(members.includes("item_e")); // Outputs: false

console.log(members.indexOf("item_b")); // Outputs: 1
console.log(members.indexOf("item_e")); // Outputs: -1 (not found)

console.log(members.lastIndexOf("item_b")); // Outputs: 3

// Search starting from a given index.
console.log(members.indexOf("item_b", 2)); // Outputs: 3 (searches from index 2 onward)

// NaN handling differs between the two methods.
var arr = [1, NaN, 3];
console.log(arr.includes(NaN)); // Outputs: true
console.log(arr.indexOf(NaN)); // Outputs: -1 (indexOf cannot detect NaN)

var validStatuses = ["active", "inactive", "pending"];
var inputStatus = "pending";
if (validStatuses.includes(inputStatus)) {
	console.log(inputStatus + " is a valid status"); // Outputs 'pending is a valid status'.
}

// Permission check: Verify whether a user has a specific role.
var userRoles = ["viewer", "editor"];
console.log(userRoles.includes("admin")); // Outputs false — user does not have admin access.
console.log(userRoles.includes("editor")); // Outputs true — user has editor access.
true
false
1
-1
3
3
true
-1
pending is a valid status
false
true

Notes

array.includes() is the best choice when you only need to know whether a value exists. Because it returns a boolean directly, you can use it as an if condition without the verbose array.indexOf() !== -1 pattern. It was added in ES2016 and has the added benefit of correctly handling NaN.

array.indexOf() has been available since ES5 and is useful when you need to know the position of a value. Because NaN === NaN evaluates to false in JavaScript, array.indexOf() cannot detect NaN. Use array.includes() to check for the presence of NaN.

These methods use strict comparison (equivalent to ===), so values of different types will not match. Objects and arrays match only if they share the same reference. For advanced searches using a condition function, use array.find() / array.findIndex().

Browser Compatibility

Chrome Chrome
52+
46 or earlier ×
Firefox Firefox
57+
42 or earlier ×
Safari Safari
18+
8 or earlier ×
Edge Edge
80+
13 or earlier ×
IE IE
11 ×
Not supported in any version
Opera Opera
48+
33 or earlier ×
iOS Safari iOS Safari
18+
8 or earlier ×
Android Browser Android Browser
52+
46 or earlier ×
Chrome Android Chrome Android
52+
46 or earlier ×
Firefox Android Firefox Android
79+
42 or earlier ×

If you find any errors or copyright issues, please .