Skip to content

JavaScript typeof Operator

Learn the basics of the JavaScript typeof Operator

In JavaScript, any value has a type assigned.

The typeof operator is a unary operator that returns a string representing the type of a variable.

Example usage:

typeof 1 //'number'
typeof '1' //'string'
typeof {name: 'Flavio'} //'object'
typeof [1, 2, 3] //'object'
typeof true //'boolean'
typeof undefined //'undefined'
typeof (() => {}) //'function'
typeof Symbol() //'symbol'

JavaScript has no “function” type, and it seems funny that typeof returns 'function' when we pass it a function.

It’s one quirk of it, to make our job easier.

If you don’t initialize the variable when you declare it, it will have the undefined value until you assign a value to it.

let a //typeof a === 'undefined'

typeof works also on object properties.

If you have a car object, with just one property:

const car = {
  model: 'Fiesta'
}

This is how you check if the color property is defined on this object:

if (typeof car.color === 'undefined') {
  // color is undefined
}

→ Get my JavaScript Beginner's Handbook

→ I wrote 17 books to help you become a better developer:

  • C Handbook
  • Command Line Handbook
  • CSS Handbook
  • Express Handbook
  • Git Cheat Sheet
  • Go Handbook
  • HTML Handbook
  • JS Handbook
  • Laravel Handbook
  • Next.js Handbook
  • Node.js Handbook
  • PHP Handbook
  • Python Handbook
  • React Handbook
  • SQL Handbook
  • Svelte Handbook
  • Swift Handbook
...download them all now!

Also, JOIN MY CODING BOOTCAMP, an amazing cohort course that will be a huge step up in your coding career - covering React, Next.js - next edition February 2025

Bootcamp 2025

Join the waiting list