DEV Community

Cover image for ๐Ÿ” Understanding `Array.includes()` in JavaScript โ€“ What Happens Under the Hood?
vetriselvan Panneerselvam
vetriselvan Panneerselvam

Posted on

๐Ÿ” Understanding `Array.includes()` in JavaScript โ€“ What Happens Under the Hood?

Hey developers ๐Ÿ‘‹,

Just another casual day discussing code with my colleagues when one of them brought up something surprisingly interesting โ€” the good old includes() method in JavaScript arrays. Most of us use it regularly, but have you ever paused and asked:

"What really happens inside includes()?"

Letโ€™s explore how it works, including edge cases and the comparison logic behind it.

๐Ÿง  What is Array.prototype.includes()?

The includes() method determines whether an array contains a specified element. It returns a boolean: true if the element is found, false otherwise.

โœ… Syntax:

array.includes(searchElement)
array.includes(searchElement, fromIndex)
Enter fullscreen mode Exit fullscreen mode
  • searchElement: The value to search for.
  • fromIndex (optional): The position in the array at which to start the search. Defaults to 0.

๐Ÿ“ฆ Example: Simple Usage

const nameList = ['ram', 'joyal', 'ayoob', 'abdul', 'vasanth', 'alan'];
console.log(nameList.includes('joyal')); // Output: true
Enter fullscreen mode Exit fullscreen mode

โš™๏ธ How It Works Internally

Letโ€™s dive into different scenarios and break them down.

๐Ÿ” Case 1: Using without fromIndex

const nameList = ['ram', 'joyal', 'ayoob', 'abdul', 'vasanth', 'alan'];
console.log(nameList.includes('ayoob')); // Output: true
Enter fullscreen mode Exit fullscreen mode

What happens:

  • nameList.length = 6
  • fromIndex = undefined
  • The method begins checking from index 0 because the fromIndex is undefined
  • 'ayoob' is found at index 2 โ†’ returns true

๐Ÿ” Case 2: Using fromIndex Within Array Length

const nameList = ['ram', 'joyal', 'ayoob', 'abdul', 'vasanth', 'alan'];
console.log(nameList.includes('ayoob', 1)); // Output: true
Enter fullscreen mode Exit fullscreen mode

What happens:

  • nameList.length = 6
  • fromIndex = 1
  • The method skips index 0 and begins checking from index 1
  • 'ayoob' is found at index 2 โ†’ returns true

๐Ÿšซ Case 3: fromIndex Exceeds Array Length

const nameList = ['ram', 'joyal', 'ayoob', 'abdul', 'vasanth', 'alan'];
console.log(nameList.includes('ayoob', 10)); // Output: false
Enter fullscreen mode Exit fullscreen mode

Why?

If fromIndex > nameList.length, the method short-circuits and returns false immediately. The array isnโ€™t even searched.

โž– Case 4: Negative fromIndex

const nameList = ['ram', 'joyal', 'ayoob', 'abdul', 'vasanth', 'alan'];
console.log(nameList.includes('ayoob', -1)); // Output: false
Enter fullscreen mode Exit fullscreen mode

Whatโ€™s going on:

  • A negative index is interpreted as array.length + fromIndex
  • In this case: 6 + (-1) = 5, so search starts at index 5
  • 'ayoob' is at index 2, so itโ€™s missed โ†’ returns false

๐Ÿคฏ Case 5: Searching for NaN

const nameList = ['ram', 'joyal', 'ayoob', 'abdul', NaN, 'alan'];
console.log(nameList.includes(NaN)); // Output: true
Enter fullscreen mode Exit fullscreen mode

Wait, what?!
You might expect this to return false because:

NaN === NaN // false
Enter fullscreen mode Exit fullscreen mode

But includes() uses the SameValueZero algorithm, which treats NaN as equal to NaN.

๐Ÿ”ฌ SameValueZero Comparison

Hereโ€™s a simplified version of how SameValueZero works:

function SameValueZero(x, y) {
  return x === y || (x !== x && y !== y); // true for NaN === NaN
}
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”„ Comparison Table

Value A Value B == Result ===Result SameValueZeroResult
1 1 โœ… true โœ… true โœ… true
1 '1' โœ… true โŒ false โŒ false
'a' 'a' โœ… true โœ… true โœ… true
0 -0 โœ… true โœ… true โœ… true
NaN NaN โŒ false โŒ false โœ… true
undefined null โŒ false โŒ false โŒ false

Important note: +0 and -0 are treated as equal in both === and SameValueZero.

๐Ÿงต Final Thoughts

Next time you're debugging a subtle bug with includes(), remember: itโ€™s not just a check โ€” it's powered by SameValueZero, and how you set fromIndex matters.

If you found this helpful, drop a โค๏ธ or leave a comment. Happy coding!

โœ๏ธ Author: Vetriselvan

๐Ÿ‘จโ€๐Ÿ’ป Frontend Developer | Code Lover | Exploring Angularโ€™s future

Top comments (2)

Collapse
 
abhinavshinoy90 profile image
Abhinav Shinoy

Nice and insightful!

Collapse
 
vetriselvan_11 profile image
vetriselvan Panneerselvam

Thanks for your feedback

Some comments may only be visible to logged-in visitors. Sign in to view all comments.