JavaScript typeof

The operator typeof returns the type of the operand passed to it. For instance, typeof 5 returns "number" and 5 is the operand.

It can only return the following 8 lowercase strings:

  1. "number"
  2. "boolean"
  3. "string"
  4. "bigint"
  5. "undefined"
  6. "function"
  7. "object"
  8. "symbol"

In some cases that will see later, it can return errors.

Type of numbers

typeof 48; // "number"
typeof 1.1; // "number"
typeof Math.Pi; // "number"
typeof Infinity; // "number"
typeof -Infinity; // "number"
typeof NaN; // "number" Yes, "not a number" is of type "number"
typeof 0b11; // "number" 3 in binary
typeof 0o11; // "number" 9 in octal
typeof 0xa; // "number" 10 in hexadecimal
typeof 2e3; // "number", 2000, scientific notation

Number('banana'); // NaN
typeof Number('banana'); // "number"
typeof Number(); // "number"

Type of booleans

typeof true; // "boolean"
typeof false; // "boolean"

Boolean(null); // false
typeof Boolean(); // "boolean", Boolean always returns a boolean
typeof Boolean(null); // "boolean"
typeof !!null; // "boolean",  double NOT operator

Read more about the double not.

Type of strings

//prettier-ignore
typeof "double"; // "string"
typeof 'single'; // "string"
typeof `back`; // "string"
typeof ''; // "string", even if empty

typeof typeof 1; // "string", same as typeof "number"
typeof String(null); // "string", String always returns a string
typeof String(); // "string", String always returns a string

Type of BigInt

typeof 10n; // "bigint"
typeof 0b10n; // "bigint", 2 in binary
typeof 0o10n; // "bigint", 8 in octal
typeof 0x10n; // "bigint", 16 in hexadecimal
typeof BigInt('5'); // "bigint", BigInt('5') is 5n
typeof BigInt(); // TypeError: Cannot convert undefined to a BigInt

Type of undefined

typeof undefined; // "undefined"
typeof undeclaredVar; // "undefined"

var unassignedVar;
typeof unassignedVar; // "undefined"

Type of functions

typeof function f() {}; // "function"
typeof function () {}; // "function", an anonymous function
typeof console.log; // "function", methods are functions too
typeof class Car {}; // "function"
// arrow function. needs parenthesis to work:
typeof (() => {}); // "function"

const g = function () {}; // A function expression
typeof g; // "function"

typeof Function('return 5'); // "function"
// Notice we are calling the function that returns 5:
typeof Function('return 5')(); // "number"

Type of symbols

typeof Symbol(); // "symbol"
typeof Symbol('some value'); // "symbol"
typeof Symbol({ k: 'v' }); // "symbol"
typeof Symbol([10]); // "symbol"
typeof Symbol(1); // "symbol"
typeof Symbol.iterator; // "symbol", iterator is also a symbol

Type of objects

typeof { key: 'val' }; // "object"
typeof {}; // "object"
typeof [1, 2, 3]; // "object", array is an object

// Notice the new operator:
typeof new Date(); // "object"

// A string representing the current date (no new operator):
typeof Date(); // "string"

// Regular expressions are objects
typeof /abc/; // "object"

typeof null; // "object", a bug, kept to not break sites

If you are curious, you can check the web archive for a proposed fix for typeof null.

Types of new

We've seen examples of using the new operator before. When used with the new operator, all constructor functions return an object. Except for the Function constructor.

typeof new String('bla'); // "object"
typeof new Number(1); // "object"
typeof new Function('return 3'); // "function

Errors

Before ECMAScript 2015, typeof would always return a string. It's not the case anymore. You will get a ReferenceError if you use let, const or class.

var v;
let l;
const c = 'hmm';
class C {}

typeof v; // "undefined"
typeof l; // "undefined"
typeof c; // "string"
typeof C; // "function"

Everything looks normal, but what if you call typeof before the declaring statements?

typeof v; // "undefined", var was "hoisted"
typeof l; // ReferenceError: l is not defined
typeof c; // ReferenceError: Cannot access 'c' before initialization
typeof C; // ReferenceError: C is not defined
typeof f; // ReferenceError: f is not defined

var v;
let l;
const c = 'hmm';
class C {}
const f = function (a) {
  return 2 * a;
};

Calling typeof

typeof is an unary operator, so you must call it like this: typeof operand. It may look like you are calling it like a function when you surround the operand with parenthesis, but in the end it's still only an operator:

typeof 1; // "number"
//prettier-ignore
typeof(1); // "number"
//prettier-ignore
1 + (1); // 2

What is happening is the parenthesis are used as a grouping operator. We've used them previously when testing an arrow function:

typeof (() => {});

Main reference material: MDN typeof

Questions

What's the console output?

console.log(typeof 21);

What's the console output?

console.log(typeof 0xac);

What's the console output?

console.log(typeof 5e2);

What's the console output?

console.log(typeof Number());

What's the console output?

console.log(typeof true);

What's the console output?

console.log(typeof false);

What's the console output?

console.log(typeof Boolean());

What's the console output?

console.log(typeof Boolean(0));

What's the console output?

console.log(typeof !!'hi');

What's the console output?

console.log(typeof 'mango');

What's the console output?

console.log(typeof 3.14);

What's the console output?

//prettier-ignore
console.log(typeof "pineapple");

What's the console output?

console.log(typeof `peach`);

What's the console output?

console.log(typeof '');

What's the console output?

console.log(typeof typeof Symbol());

What's the console output?

console.log(typeof String());

What's the console output?

console.log(typeof String(undefined));

What's the console output?

console.log(typeof 1n);

What's the console output?

console.log(typeof 0b1n);

What's the console output?

console.log(typeof 0o1n);

What's the console output?

console.log(typeof 0x1n);

What's the console output?

console.log(typeof BigInt('32'));

What's the console output?

console.log(typeof BigInt(23));

What's the console output?

console.log(typeof BigInt());

What's the console output?

console.log(typeof undefined);

What's the console output?

console.log(typeof x);

What's the console output?

var x;
console.log(typeof x);

What's the console output?

function a() {}
console.log(typeof a);

What's the console output?

const result = typeof function () {
  return 'much wow';
};
console.log(result);

What's the console output?

console.log(typeof Math.cos);

What's the console output?

console.log(typeof Math.Pi);

What's the console output?

console.log(typeof class EmptyClass {});

What's the console output?

const arrow = () => console.log('hey');
console.log(typeof arrow);

What's the console output?

const e = function () {};
console.log(typeof e);

What's the console output?

const e = function () {
  return 2;
};
console.log(typeof e());

What's the console output?

const f = Function('a', 'return Boolean(a);');
console.log(typeof f);

What's the console output?

console.log(typeof Symbol());

What's the console output?

console.log(typeof Symbol([1, 2]));

What's the console output?

console.log(typeof Symbol('ho'));

What's the console output?

console.log(typeof Symbol(2));

What's the console output?

console.log(typeof { 1: 1 });

What's the console output?

console.log(typeof Infinity);

What's the console output?

console.log(typeof {});

What's the console output?

console.log(typeof [5]);

What's the console output?

console.log(typeof new Date());

What's the console output?

console.log(typeof Date());

What's the console output?

// it's a regular expression
console.log(typeof /hello/);

What's the console output?

console.log(typeof null);

What's the console output?

console.log(typeof new String(1));

What's the console output?

console.log(typeof new Number('hey'));

What's the console output?

console.log(typeof x);
var x = 1;

What's the console output?

console.log(typeof x);
const x = 1;

What's the console output?

console.log(typeof -Infinity);

What's the console output?

console.log(typeof x);
let x = 1;

What's the console output?

console.log(typeof C);
class C {}

What's the console output?

console.log(typeof a);
const a = function () {};

What's the console output?

console.log(typeof (() => {}));

What's the console output?

console.log(typeof NaN);

What's the console output?

console.log(typeof 0b101);

What's the console output?

console.log(typeof 0o15);