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

Comparing With === Instead of ==

Comparing With === Instead of ==

In this lesson, you ported the backup check’s one comparison and saw that JavaScript’s == converts types before comparing, as + and * do, where Python’s == doesn’t.

  • Python’s == never treats text as equal to a number: the retry count as the text "5" and as the number 5 compare False. In the port, process.env holds the settings, and as in os.environ, every value that’s set is text. The retry count goes through Number(), its default for an unset variable left for later.
  • The flag’s line, ported with ==, works on the new server, because both sides of its comparison are text. When both sides are text, == compares the way Python’s does. JavaScript’s true and false are lowercase, where Python writes a capital.
  • When the types don’t match, == converts first, so "5" == 5 is true. As a test of how far that goes, "0" == false is true too: == turns both sides into numbers, false into 0 and the text "0" into 0. Python also treats False as zero, but it never turns text into a number.
  • "0" == false is a comparison, not a truth test: in an if, the text "0" still counts as true. Empty text turns into zero as well, as it does with Number(), so 0 == "" is true. Python says False to "5" == 5, "0" == False, and 0 == "" alike.
  • With ==, text passes for a number: a retry count left as text would still equal 5, so the comparison hides the missing conversion.
  • ===, the operator you’ve used to check for null and undefined, never converts. For text, numbers, and booleans, it’s true only when both sides have the same type and the same value, so text never equals a number, as in Python. "5" === 5, "0" === false, and 0 === "" are all false. == is called loose equality, and === strict equality. The full rules for loose equality come later.
  • The flag’s line worked with ==, but === gives Python’s answers here, and you don’t have to check both sides’ types to trust it, so it’s what JavaScript developers write by default. To compare a setting as a number, convert it with Number() first: Number("5") === 5 is true.
  • Not-equals comes in the same two kinds: != converts, so "5" != 5 is false, and !== doesn’t, so "5" !== 5 is true, as Python’s "5" != 5 is True.
  • The rule: write === and !== everywhere, and convert text with Number() before you compare it as a number. Loose equality’s one use on purpose is x == null, which catches null and undefined together.

Here’s the port’s flag and retry count, written the JavaScript way:

Language: JavaScript
const dryRun = process.env.DRY_RUN === "1";
const retries = Number(process.env.BACKUP_RETRIES);

console.log(`Dry run: ${dryRun}`);
console.log(`Up to ${retries + 1} attempts`);

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:02 In the last lesson, + and * converted between text and numbers. In this lesson, you’ll see that JavaScript’s looser == converts too, and which operator to write instead.

00:16 The Python check sets the dry-run flag by comparing the variable with the text one. Python’s == never treats text as equal to a number. The retry count is the text five before int() and the number five after, and those two compare False.

00:36 In the port, process.env holds the settings, and as in os.environ, every value that’s set is text. The flag’s line keeps Python’s ==.

00:49 The retry count goes through Number(), its default left for later. The prints become console.log() lines. Run both on the new server, and they print the same two lines.

01:02 JavaScript’s true and false are lowercase, where Python writes a capital.

01:09 The port works because both sides of the flag’s comparison are text: the variable’s value and the text in quotes. When both sides are text, == compares the way Python’s does.

01:22 Now compare the retry count’s two types, the text five and the number five. JavaScript says true. == converted the text to a number before comparing, as * did.

01:37 How far does the conversion go? Test it on the text zero against the boolean false, a pair the check never compares. Python says they aren’t equal.

01:49 What does JavaScript’s == say? Your turn: pause the video and predict.

02:00 It says true. == turns both sides into numbers. false becomes zero, and so does the text zero. Python also treats False as zero, but it never turns text into a number.

02:18 The text zero against false is a comparison, not a truth test: in an if, the text zero still counts as true. Empty text turns into zero as well, as Number() does.

02:31 So zero and empty text are equal, where Python says False.

02:37 With ==, text passes for a number. A retry count left as text would still equal five, so the comparison hides the missing conversion.

02:49 ===, which you’ve used to check for null and undefined, never converts. For text, numbers, and booleans, it’s true only when both sides have the same type and the same value.

03:03 So text never equals a number, as in Python. With ===, all three questions say false, as Python’s do. == is called loose equality, and === strict equality.

03:19 The full rules for loose equality come later.

03:23 In the port, the flag’s line switches to ===. It worked, but === never converts, so it gives Python’s answers here, and you don’t have to check both sides’ types to trust it.

03:36 That’s why JavaScript developers write it by default. Run it again, and it prints the same two lines. To compare a setting as a number, convert it with Number() first, and the converted five equals five.

03:52 In JavaScript, not-equals comes in the same two kinds. The loose one, !=, converts, so for the text five and the number five it says false.

04:06 The strict one, !==, says true, as Python’s != does.

04:15 Write the strict operators everywhere, and convert first to compare numbers. Loose equality’s one use on purpose is == null, which catches null and undefined together.

04:28 Next up is the exercise, Read the Retry Count and Dry-Run Flag. From the new server’s settings, given as text, you’ll print the job’s attempts, its minutes of retrying, and whether tonight’s run is a dry run, matching the Python check.

Become a Member to join the conversation.