The investigation report for your Python teammate is nearly done, but one line raises a question. Before the processor totals tonight’s Falcons and Owls rows, it checks the points column with points.includes(NaN) because points that fail to parse arrive as NaN. Your teammate asks why that check doesn’t use .indexOf() like the other lookups.
In Python, a list’s in and .index() match elements by the same rule, even though one returns a Boolean and the other a position. In JavaScript, each method runs its own algorithm. In this lesson, you’ll read those algorithms and distinguish what they specify from what V8 does to run them fast.
Trace .includes() Through SameValueZero
In tonight’s log, the Owls’ 2 failed to parse, so the points column holds a NaN in the middle. Your teammate wouldn’t trust Python’s in with that column:
import math
points = [3, float("nan"), 4]
print(float("nan") in points, points[1] in points)
print(any(math.isnan(p) for p in points))
The first two lookups ask what looks like one question and get two answers:
False True
True
For a list, Python’s in checks identity first, then ==, and nan == nan is False, so your teammate writes the math.isnan() check instead.
In JavaScript, .includes() returns whether an array holds a value, and .indexOf() returns its position or -1. Here are both on the same column: