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)
-
searchElement: The value to search for. -
fromIndex(optional): The position in the array at which to start the search. Defaults to0.
๐ฆ Example: Simple Usage
const nameList = ['ram', 'joyal', 'ayoob', 'abdul', 'vasanth', 'alan'];
console.log(nameList.includes('joyal')); // Output: true
โ๏ธ 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
What happens:
nameList.length = 6fromIndex = undefined- The method begins checking from index
0because the fromIndex is undefined -
'ayoob'is found at index2โ returnstrue
๐ Case 2: Using fromIndex Within Array Length
const nameList = ['ram', 'joyal', 'ayoob', 'abdul', 'vasanth', 'alan'];
console.log(nameList.includes('ayoob', 1)); // Output: true
What happens:
nameList.length = 6fromIndex = 1- The method skips index
0and begins checking from index1 -
'ayoob'is found at index2โ returnstrue
๐ซ Case 3: fromIndex Exceeds Array Length
const nameList = ['ram', 'joyal', 'ayoob', 'abdul', 'vasanth', 'alan'];
console.log(nameList.includes('ayoob', 10)); // Output: false
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
Whatโs going on:
- A negative index is interpreted as
array.length + fromIndex - In this case:
6 + (-1) = 5, so search starts at index5 -
'ayoob'is at index2, so itโs missed โ returnsfalse
๐คฏ Case 5: Searching for NaN
const nameList = ['ram', 'joyal', 'ayoob', 'abdul', NaN, 'alan'];
console.log(nameList.includes(NaN)); // Output: true
Wait, what?!
You might expect this to return false because:
NaN === NaN // false
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
}
๐ 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)
Nice and insightful!
Thanks for your feedback
Some comments may only be visible to logged-in visitors. Sign in to view all comments.