3 ms·
> It's kinda sad that JavaScript needs so many lines of code to figure out if a number is even, but understandable due to its dynamic nature. Except it doesn't
by dragonwriter 6y ago
> It's kinda sad that JavaScript needs so many lines of code to figure out if a number is even, but understandable due to its dynamic nature.
Except it doesn't need that many, and what it does need is due to the choice of main numeric type, not it's dynamic nature; almost all the line count in is-odd is about producing superfluously detailed error messages in the case of invalid values, and that includes the is-number dependency. The only check actually needed to deal with input for which the substantive check is incorrect is the isSafeInteger check (which subsumes all the others), which would be needed even if JS was a static-typed language, since this function operates by design on the Number (IEEE-754 double precision binary float) type, not the more recent BigInt type.