The typeof Operator in JavaScript

The typeof Operator in JavaScript

Ferenc Almasi β€’ 2020 December 10 β€’ πŸ“– 1 min read

In JavaScript, the typeof operator can be used for getting the data type of its operand as a string:

// The typeof operator is used for getting
// the type of its operand:

// This will return "string"
typeof '1'

// This will return "number"
typeof 1

// This will return "number"
typeof NaN

// This will return "boolean"
typeof true

// This will return "undefined"
typeof undefined

// This will return "symbol"
typeof Symbol()

// This will return "object"
typeof []

// This will return "object"
typeof null
typeof.js
Copied to clipboard!

Note that the type of NaN is a number. Also the type of null is an object. If you are interested in the history of why null is an object, I highly recommend checking out the below article from 2ality:

The history of "typeof null"

What is the typeof operator in JavaScript?
If you would like to see more Webtips, follow @flowforfrank

50 JavaScript Interview Questions
Looking to improve your skills? Check out our interactive course to master JavaScript from start to finish.
Master JavaScript

Resources:

Did you find this page helpful?
πŸ“š More Webtips
Frontend Course Dashboard
Master the Art of Frontend
  • check Unlimited access to hundred of tutorials
  • check Access to exclusive interactive lessons
  • check Remove ads to learn without distractions
Become a Pro

Recommended