Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing a variable with itself

I stumbled over this polyfill of Array.prototype.includes. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes. Is there a reason for the comparison of the variables with themselves on line 21,22?

if (searchElement === currentElement ||
         (searchElement !== searchElement && currentElement !== currentElement)) {
  return true;
}
like image 645
stoeffel Avatar asked Nov 26 '14 08:11

stoeffel


People also ask

How do you compare variables?

Use scatterplots to compare two continuous variables. Use scatterplot matrices to compare several pairs of continuous variables. Use side-by-side box plots to compare one continuous and one categorical variable. Use variability charts to compare one continuous Y variable to one or more categorical X variables.

How do you compare two variables to equal?

Use the equality operator to check if multiple variables are equal, e.g. if a == b == c: . The expression will compare the variables on the left-hand and right-hand sides of the equal signs and will return True if they are equal.

How do you compare multiple values in an if statement?

To check if a variable is equal to all of multiple values, use the logical AND (&&) operator to chain multiple equality comparisons. If all comparisons return true , all values are equal to the variable. Copied! We used the logical AND (&&) operator to chain multiple equality checks.


1 Answers

Yes, this second operand of the || does check whether both searchElement and currentElement are NaN - the only value in JavaScript that is not === to itself. includes is supposed to use the SameValueZero equivalence algorithm, which is different from the the Strict Equality Comparison Algorithm (used by ===) or the SameValue algorithm (used in Object.is).

like image 132
Bergi Avatar answered Oct 01 '22 09:10

Bergi