CuteMachine

The JS Typeof Operator

In this article, you will learn everything you need to know about JavaScript's typeof operator.

You can run all examples in this article with the excellent RunJS REPL.1

Types In JavaScript

When it comes to types, you will often read that eight different data types exist in JavaScript. But to be more precise, there are five primitive data types, two special values null and undefined, and objects.

It is worth mentioning that JavaScript is dynamically typed. That means the type is not bound to variables but values. A variable can hold a value of any type. The following code is valid JavaScript, of course.

let num = 42
console.log(num)    // 42

num = 'forty-two'
console.log(num)    // "forty-two"

Primitive Data Types

Primitive data types or primitive values are often just called primitives in JavaScript. There are five primitives in JavaScript:

  • bigint
  • boolean
  • number
  • string
  • symbol

The Primitive bigint

When I examined the different types in JavaScript, I was surprised to see a primitive type bigint. Notice the n at the end of the number. This extension tells JavaScript that the number is a bigint.

{
  typeof(98n)    // "bigint"
}

Interestingly this does not work from inside RunJS. Run the example in the Firefox dev console instead. Or use the BigInt constructor like in the following example to see it working inside of RunJS.

const bigFoo = BigInt("9007199254740995")
typeof bigFoo    // "bigint"

The Special Values null And undefined

null is a special value and not a primitive. It is a separate type that tells that there is no value.

typeof(null)    // 'object'

Yes, typeof(null) returns "object". This is considered a bug in JavaScript. null is not an object; it is a special value with its own type, which means there is no value.

undefined is yet another special value that is similar to null and tells us that there is no value.

typeof(undefined)    // 'undefined'

Interestingly null is a reserved keyword, but undefined is not; it is a property of the global object.

In my programs, I never assign undefined to a variable. When I need to indicate that a variable does not hold a value, I assign null. With this distinction, I can judge whether the absence of a value is intentional.

The typeof Operator

The typeof operator takes one operand and returns the type of that operand. The operand is the object or primitive for which we want to know the type. You can use the operator with or without parentheses.

typeof 42     // "number"
typeof(42)    // "number"

I prefer to use the second version, as it is more explicit.

How to Find The Type Of A Variable Or Value

To find the type of a value (or the value assigned to a variable for that matter) you just give the value as the operand to the typeof operator like so:

function foo() {
  // do something
}
const obj = {}
const arr = [1, 2, 3]

typeof(42n)            // 'bigint'
typeof(false)          // 'boolean'
typeof(3)              // 'number'
typeof('hello')        // 'string'
typeof(Symbol('123'))  // 'symbol'
typeof(foo)            // 'function'
typeof(obj)            // 'object'
typeof(arr)            // 'object'
typeof(null)           // 'object'
typeof(undefined)      // 'undefined'

The highlighted line shows the result for the value null, which is a bug in JavaScript.

It is also worth mentioning that there is a distinction between functions and objects, although functions in JavaScript are objects too.


  1. Run Eval Print Loop

Posted on CuteMachine.

Jo's Profile ImageWritten by Jo who lives and works in Frankfurt building digital doodah. Stalk him on Twitter.

TOS • Privacy • Disclaimer