Telling null From undefined

On the new server, nobody set BACKUP_BUCKET. The sysadmin’s Python check caught that at once. bucket is None was true, so it printed its refusal and stopped with exit status 1. The JavaScript port has to recognize an unset variable as reliably, and that’s harder than it looks, because Python has one value for “nothing here” and JavaScript has two.

In this lesson, you’ll port the is None checks and learn which “nothing” value to expect where. Start with the lines that read the settings.

Read the Settings From process.env

The Python check reads each setting with os.environ.get(), which gives None for an unset variable, and keeps a placeholder that the job fills in after a successful backup:

Language: Python
import os

bucket = os.environ.get("BACKUP_BUCKET")
region = os.environ.get("BACKUP_REGION")
notify_email = os.environ.get("NOTIFY_EMAIL")
last_success = None

In the JavaScript port, the settings come from process.env, and the placeholder starts as null:

Language: JavaScript
const bucket = process.env.BACKUP_BUCKET;
let region = process.env.BACKUP_REGION;
const notifyEmail = process.env.NOTIFY_EMAIL;
let lastSuccess = null;

process.env is the Node.js counterpart of os.environ, not part of the JavaScript language, and every value that’s set is a string. Dot access on it works like os.environ.get(), not os.environ["BACKUP_BUCKET"], so a missing variable never raises a KeyError.

null means “deliberately empty,” and it’s what None becomes when your code sets a placeholder. As you saw with typeof, typeof null says "object", a historical quirk, so test for null with === rather than typeof.

Watch an Unset Bucket Slip Past

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

Become a Member to join the conversation.