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

Checking for Empty Values

Checking for Empty Values

In this lesson, you ported the two checks that warn when nobody will hear about a failed backup: the one for an empty NOTIFY_EMAIL, and the one for an empty recipients list in the new server’s settings file.

  • Where Python writes not, JavaScript writes ! in front of the value. Like not, it answers true or false, in lowercase. Inside the if, console.log() does print()’s job.
  • For strings, null, and undefined, truthiness carries over: an empty string and undefined count as false, as an empty string and None do in Python. So the notify check warns with NOTIFY_EMAIL empty or unset, as the Python does, and the message’s “empty” covers unset too.
  • Values that count as false in a condition are falsy, and every other value is truthy. MDN’s Falsy page keeps the full list.
  • JavaScript’s falsy values are false, 0, the empty string "", null and undefined, and NaN, the “not a number” value you get by adding one to a missing retry count. Two rare ones also count: negative zero (-0) and the zero of bigint (0n), a type for huge whole numbers you won’t need here.
  • Any text that isn’t empty is truthy, even "false", in both languages.
  • On JavaScript’s falsy values, Python agrees on all but one: its float("nan") is truthy, but NaN is falsy in JavaScript. Python’s own falsy values also include its empty containers, such as an empty list or dict, and that’s where the two languages part.
  • An array is an object in JavaScript, empty or not, and every object is truthy. Python asks a list or a dict whether it’s empty, but JavaScript’s falsy list is a fixed set of primitive values, plain values like strings and numbers, and it never looks inside an object. So !job.recipients is false for an empty array, and the line-by-line port of if not job["recipients"]: prints nothing.
  • Ask the question you mean instead: an array’s .length counts its items, like Python’s len(), so job.recipients.length === 0 is true for an empty array. Where Python’s habit is if not, a JavaScript developer checks the length.
  • An empty object is truthy too, and its .length is undefined, so checking it never warns either. Count its keys instead, as len() does for a dict: Object.keys(obj).length === 0, where Object.keys() gives you an array of an object’s keys, and you check the array’s length is zero. Objects get a course of their own later on.
  • The rule: for a string or a missing value, if (!notifyEmail) works as Python’s if not notify_email: does. For an array, check its length: job.recipients.length === 0. For an object, count its keys: Object.keys(obj).length === 0.

Here’s the notify check in the port:

Language: JavaScript
const notifyEmail = process.env.NOTIFY_EMAIL;

if (!notifyEmail) {
  console.log(
    "Warning: NOTIFY_EMAIL is empty,",
    "so nobody hears about failures.",
  );
}

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 this lesson, you’ll port the backup check’s next two checks, which warn when nobody will hear about a failed backup, and one of them won’t carry over line by line.

00:13 The first reads the address to notify. Python’s not treats an empty string and None as false. So where the address is empty, the check prints its warning.

00:26 The port reads the address from process.env instead. Where Python writes not, JavaScript writes ! in front of the value.

00:36 Like not, it answers true or false, in lowercase. With console.log() doing print()’s job, run both with the address empty, and the port prints the same warning.

00:50 With the variable unset, both warn too, so the message’s empty also means unset. That part carries over: an empty string and undefined count as false, as an empty string and None do in Python.

01:07 Values that count as false in a condition are called falsy, and every other value is truthy. Most of JavaScript’s short list matches Python’s: null and undefined, false, zero, and the empty string.

01:23 Two rare ones also count: negative zero, and the zero of bigint, a type for huge whole numbers you won’t need here. On the whole list, Python agrees on all but one.

01:37 NaN, the not a number value from adding one to a missing retry count, is falsy here, but Python’s float("nan") is truthy. And any non-empty text is truthy, even the word false, in both languages.

01:53 The second check reads the new server’s settings file, written out here. Its recipients list is empty. Python’s not treats an empty list as false, so the check prints its warning.

02:06 In the port, the file becomes an object, and the list an array. Line by line, not turns into !, as it did for the address. The warning inside stays as it was.

02:21 The recipients array is empty. So does the port warn? Your turn: pause the video and predict.

02:34 It prints nothing. The port stays silent, so nobody would hear about a failed backup.

02:42 Ask ! about the recipients: it says false, so the empty array counts as true. Python’s not says True here.

02:53 That’s because an array is an object in JavaScript, and every object is truthy. Python asks a list or a dict whether it’s empty. JavaScript’s falsy list is a fixed set of primitive values, plain values like strings and numbers, and it never looks inside an object.

03:13 So Python’s empty list is falsy, but no array or object is on JavaScript’s list.

03:21 Ask the question you mean: is the array empty? An array’s .length counts its items, like Python’s len(), and .length === 0 is true for the recipients.

03:33 With that comparison as the if’s condition, run the port again. It prints the warning, as the Python does. Watch out: an empty object is truthy too, and its .length is undefined, so checking it never warns either.

03:49 Count its keys instead, as len() does for a dict: Object.keys() gives you an array of them, and you check its length is zero. Objects get a course of their own later on.

04:02 So here’s the rule. For a string or a missing value, an if with ! works as Python’s if not does. For an array, where a Pythonista writes if not, a JavaScript developer checks the length.

04:18 For an object, you count its keys first. Next up is the exercise Warn When Nobody Will Hear About Failures. You’ll take three servers’ notify settings and print a warning for each where nobody will hear about a failed backup, and none for the server that’s set up properly.

Become a Member to join the conversation.