Step 1 of 7 · Reading · ~3 min
Learn
Modern Syntax
Equality: == vs === and Type Coercion
JavaScript quietly converts values from one type to another in many situations. This is called coercion. By the end of this lesson you can predict when it happens, use === so it does not happen by accident, and convert text to numbers on purpose with the exact rules Number() follows.
Strict equality: ===
=== is true only when both sides have the same type and the same value. Nothing is converted.
Objects and arrays are equal only to themselves: [] === [] is false, because each literal creates a new array.
Loose equality: ==
== first converts the operands to a common type, then compares. The rules are long and several results are surprising:
Do not memorise the table. Use === and !== everywhere. The only widely accepted use of == is x == null, which is true for both null and undefined.
Other places coercion happens
The + operator. If either side is a string, + joins; otherwise it adds. Every other arithmetic operator converts to numbers:
Relational operators. Two strings compare alphabetically, character by character, which is not numeric order; with a number on one side, the other becomes a number:
Conditions treat falsy values (false, 0, "", null, undefined, NaN) as false and everything else, including "0" and [], as true.
How Number() converts text
Input from stdin is text, so Number(text) is how it becomes a number. The rules:
| text | Number(text) | note |
|---|---|---|
"42", " 42 " | 42 | spaces around the digits are ignored |
"-3.5", "+7" | -3.5, 7 | one leading sign |
".5", "5." | 0.5, 5 | |
"2e3" | 2000 | exponent |
"0xff" | 255 | hex, with the 0x prefix (also 0b, 0o) |
"", " " | 0 | empty text is zero, not NaN |
"12px", "1,5", "abc" | NaN | the whole text must be a number |
"Infinity" | Infinity |
Two helpers finish the job. Number.isNaN(x) tests for NaN without converting. Number.isFinite(x) is true only for real numbers that are neither NaN nor infinite, again with no conversion. The old global functions isNaN and isFinite convert their argument first, so isNaN("abc") is true but isNaN("") is false.
parseInt("12px", 10) and parseFloat("3.5kg") read a leading number and ignore the rest. That is lenient where Number is strict; choose deliberately.
Object.is
Object.is(a, b) is like === with two corrections: Object.is(NaN, NaN) is true, and Object.is(0, -0) is false.
Common mistakes
- Using
==and relying on a conversion you have not thought through. "1" + 2giving"12"when you meant 3: convert input withNumber()before adding.- Sorting or comparing numeric strings:
"10" < "9"is true. - Assuming
Number("")isNaN. It is0, which silently lets an empty field through as a valid number. - Using
x === NaN. It is never true. UseNumber.isNaN(x).
Your exercise
Filter tokens by what Number() makes of them. The starter splits the line, and prints the kept tokens or none. You write the decision of which tokens to keep.
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…