I recently stumbled upon a bug caused by .includes()
method in javascript. I wanted to return the exact result in respect to case-insensitive but for some reason this does not works
["Ford", "BMW", "Fiat"].includes("bmw");
//returns false
//expected result true
I then fixed my issue with the help of .find()
like below:
!!["Ford", "BMW", "Fiat"].find(el => el.toUpperCase() === "bmw".toUpperCase());
//returns true
Can someone help me understand why includes
does not work in case-insensitive situations ?
CodePudding user response:
Why would it work case-insensitively? Quoth the MDN:
The
includes()
method determines whether an array includes a certain value among its entries, returning true or false as appropriate. [...] Note: Technically speaking, includes() uses thesameValueZero
algorithm to determine whether the given element is found.
sameValueZero
is defined to be Object.is
but with 0
and -0
being equal.
That being the case, we can try that out:
> Object.is("bmw", "BMW")
false
> Object.is("bmw", "bmw")
true