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 = 6
fromIndex = undefined
- The method begins checking from index
0
because 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 = 6
fromIndex = 1
- The method skips index
0
and 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 |
SameValueZero Result |
---|---|---|---|---|
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.