Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Reading == by Its Rules

Reading == by Its Rules

In this lesson, you read the co-op’s old dashboard page, which uses == everywhere, so that you know what each == does before anyone replaces it. Its rainLabel() function labels school’s rain, sent as the text "0.0" by old firmware, as dry, although Python’s "0.0" == 0 is False. == follows six rules:

  • Same type: == compares like ===. 0.2 == 0 is false and 0 == 0 is true. JavaScript’s booleans, true and false, are lowercase, where Python writes True and False.
  • null and undefined equal each other and nothing else, so null == 0 is false, much as Python’s None equals only None.
  • A string next to a number becomes a number, so "0.0" == 0 is true. Python’s == works across number types only, as in 1 == 1.0, and a string never equals a number.
  • A boolean becomes a number first: true is 1 and false is 0. So true == 1 is true, as Python’s True == 1 is, but true == "1" is true as well, where Python’s True == "1" is False. Watch out: == true isn’t a truthiness test, in Python either, where 2 == True is False. Orchard’s old firmware sends its heater’s power level, 2, which is truthy, but true becomes 1, and 2 == true is false.
  • An object next to a primitive becomes a primitive first: an empty array becomes an empty string, so [] == "" is true. How that works in full is a later topic.
  • NaN equals nothing.

Replacing if (rain == 0) with if (Number(rain) === 0), converting first and then comparing strictly as float(rain) == 0 would in Python, labels orchard’s missing reading dry. Number(null) is 0, while Python’s float(None) raises a TypeError. The old line never converted null: == checks for null and undefined before any conversion, so null == 0 was false.

With a number on the other side, the relational operators <, >, <= and >= take the converting path, so a check like rain >= 0 lets orchard’s missing reading through: null >= 0 and null <= 0 are both true. Watch out: they turn null into 0 and undefined into NaN, which fails every comparison, with no error, where Python’s None >= 0 raises a TypeError.

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 In this lesson, you’ll read the co-op’s old dashboard page, which uses == everywhere, before anyone replaces it. The page logs each station’s 12:00 label.

00:13 School’s old firmware sends its rain as text, "0.0", and the page still labels it dry. In Python, "0.0" == 0 gives False.

00:29 == follows six rules. First, when both sides have the same type, == compares like ===. JavaScript’s true and false are lowercase.

00:43 Second, null and undefined equal each other and nothing else, as Python’s None equals only itself. Third, a string next to a number becomes a number.

00:55 School’s "0.0" turns into 0, so the page says dry.

01:01 Python’s == works across number types only: 1 == 1.0 is True, but a string never equals a number.

01:12 Fourth, a boolean turns into a number first: true becomes 1 and false becomes 0. So true == 1 is true, as Python’s True == 1 is.

01:25 In true == "1", the 1 then meets a string, so it’s true as well. In Python, True == "1" gives False.

01:37 Watch out: as in Python, == true isn’t a truthiness test. Orchard’s old firmware sends its heater’s power level, 2, which is truthy, but true becomes 1, and 2 isn’t 1.

01:52 Fifth, an object next to a primitive becomes a primitive first. An empty array becomes an empty string, so [] == "" is true; the full story comes later.

02:07 Sixth, NaN equals nothing.

02:11 Now replace the zero test: new JavaScript code uses ===. By the third rule, == turned school’s text into a number first. So you convert with Number(), then compare strictly, like Python’s float(rain) == 0.

02:29 The == null line stays: it catches null and undefined together. What does the port log for orchard’s missing reading now?

02:39 Your turn: pause the video and predict.

02:47 Orchard comes out dry. The port now says a station with no rain data had no rain.

02:56 Here’s why: Number(null) is 0, not an error. Python’s float(None) would raise a TypeError there. The old line’s == checks for null and undefined before any conversion.

03:11 So null == 0 was false, and orchard fell through to the == null test. With a number on the other side, the relational operators take the converting path instead.

03:24 So a check like rain >= 0 would let orchard’s missing reading through: null >= 0 and null <= 0 are both true.

03:35 Watch out: they turn null into 0 and undefined into NaN, which fails every comparison, with no error, where Python’s None >= 0 raises a TypeError instead.

03:50 The fix says out loud what the old line did: missing first. The == null test moves to the top. Then the zero test converts what’s left and compares strictly.

04:03 With === only, == null splits into two checks, one per missing value. Run it, and the port logs the old page’s four labels again, orchard’s included.

04:17 When you read ==, work it out with the rules: null and undefined first, then numbers. When you write, === is the everyday operator, and == null the one idiom to keep.

04:31 When you replace a ==, check for a missing value before you convert.

04:37 Next up is the exercise Label the Rain and the Heaters. There, you’ll print each station’s 12:00 rain label and whether its anti-icing heater is on, so that every station gets the right label.

Become a Member to join the conversation.