JavaScript Variables and Types for Pythonistas (Summary)

Kestrel Print’s backup check now runs in JavaScript. On the new server, where nobody set BACKUP_BUCKET, backup-check.js refuses to start and exits with status 1, as the Python check does, instead of backing up to a bucket called undefined. In all three runs, it prints the same lines as Python, except that the debug line says string where Python says str.

You can now:

  • Declare each name once, with const by default and let only for a name the code reassigns
  • Declare a name at the level where it’s read, because a let or const declaration inside braces lives only inside them
  • Check a value’s type with typeof and spot an array with Array.isArray()
  • Tell null from undefined and catch either with == null
  • Test for an empty array with .length === 0 and an empty plain object with Object.keys(obj).length === 0, since both are truthy even when empty
  • Convert text with Number() once and compare with ===

Keep and Drop Your Python Habits

You ported most of the check without changing its logic. process.env.NAME does the job of os.environ.get(), but an unset variable gives undefined where Python gives None. An empty string is falsy in both languages, so if (!notifyEmail) warns as if not notify_email: does.

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.