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

Asking a Value's Type With typeof

Asking a Value’s Type With typeof

In this lesson, you ported two lines of the backup check that ask about a value’s type: the debug line, and the check that makes sure the settings file’s recipients are a list, not one address typed as a lone string.

  • JavaScript has no type() function. You write typeof in front of a value instead. It’s an operator rather than a function, like Python’s not, so it needs no parentheses.
  • Python’s type() gives you the class, so the debug line asks for its .__name__. typeof skips the class and answers with the name as a string, and the names are JavaScript’s own: string where Python says str.
  • The type names: int and float are both "number", so typeof 2.5 is "number" too. str is "string", and bool is "boolean", whose value is lowercase true. Where Python has None, JavaScript has undefined and null. Two more, bigint and symbol, complete the list of primitive types.
  • To check a type, compare the answer with the name in quotes: typeof retries === "number" is true, as isinstance(retries, int) is True in Python. Python writes a type bare, but typeof’s answer is a string: without the quotes, number is an unknown name, and JavaScript stops with a reference error, its version of a NameError.
  • A primitive is a basic value rather than an object, where in Python even 3 is an int object. JavaScript lends a number methods such as .toFixed() when you call one.
  • typeof has an answer for each primitive type, and says "object" for almost everything else, arrays included. So typeof job.recipients is "object", for an array as for a plain object like job, and the "array" check never passes. Functions get an answer of their own, "function".
  • typeof null is "object" too, a long-standing quirk, so a check for "object" lets null through.
  • To ask whether a value is an array, use Array.isArray(), a method of JavaScript’s built-in Array. For the recipients, it says true, as isinstance() does in Python.
  • typeof is like type() only partway: type() and isinstance() work with any class, your own included, while typeof only picks a name from its fixed list.

Here’s the debug line in the port, with the settings held in constants for now:

Language: JavaScript
const bucket = "kestrel-db-backups";
const retries = 3;
const dryRun = true;

console.log(`Debug: bucket is ${typeof bucket}`);

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 two lines of the backup check that ask about a value’s type, and one of them won’t carry over line by line. The first is the debug line, which prints the bucket’s type.

00:16 Python asks type() and puts the type’s name, str, into the line. JavaScript has no type() function.

00:26 The port holds the settings in constants for now. Then you write typeof in front of the value. It’s an operator, like Python’s not, rather than a function, so it needs no parentheses.

00:41 Python’s type() gives you the class, so the line asks for its .__name__. But typeof skips the class and answers with the name as a string.

00:52 Run the port, and it prints string as the bucket’s type. Python said str: the names are JavaScript’s own.

01:03 The retry count is a number. So is a decimal like two point five, because JavaScript has one number type where Python has int and float.

01:14 The dry-run flag is a boolean. Its value is lowercase true, where Python writes a capital.

01:22 To check a type, compare the answer with the name in quotes. Asked whether the retry count is a number, JavaScript says true, as isinstance() does in Python.

01:34 Watch out: Python writes a type bare, but typeof’s answer is a string, so without the quotes, number is an unknown name, and JavaScript stops with a reference error, its version of a name error.

01:49 The check also reads the job’s settings file, written out here. Its recipients get the failure emails, so the Python makes sure they’re a list, not one address typed as a lone string.

02:01 For this file, it prints recipients OK. In the port, the file becomes an object, and the list an array. So isinstance() turns into a typeof check against "array", in quotes.

02:18 The recipients are an array. What does the port print? Your turn: pause the video and predict.

02:30 It prints the warning: recipients is not a list.

02:35 The port complains about a file that’s right.

02:40 Asked about the recipients, typeof says "object". So this check can never pass: typeof has an answer for each of a short, fixed list of primitive types, and says "object" for almost everything else, arrays included.

02:56 A primitive is a basic value rather than an object, where in Python even 3 is an int object. JavaScript lends a number methods like .toFixed() when you call one.

03:10 The three you’ve met are on the list. Where Python has None, JavaScript has undefined and null, which gets its own lesson next.

03:19 Two more, bigint and symbol, complete the list. Anything else is an object, arrays included, so typeof says "object" for an array and for a plain object like job.

03:35 Functions get an answer of their own: typeof calls them "function". Watch out: typeof null says "object" too, a long-standing quirk, so a check for "object" lets null through.

03:50 To check for an array, JavaScript’s built-in Array has a method, .isArray(). For the recipients, it says true, as isinstance() does.

04:01 With it replacing the typeof check, run the port again. It prints recipients OK, like the Python. So typeof is like type() only partway.

04:13 Python’s type() and isinstance() work with any class, your own included, while typeof only picks a name from its list.

04:23 The rule: for any primitive but null, ask typeof, and compare its answer with a string. For an array, ask Array.isArray().

04:35 Next up is the text lesson Telling null From undefined. JavaScript has two values for nothing. You’ll find out which one an unset variable gives you.

Become a Member to join the conversation.