Blog Tech Ensuring Data Integrity with Javascript Data Types and the Javascript Typeof Operator

Ensuring Data Integrity with Javascript Data Types and the Javascript Typeof Operator

Javascript is a dynamic language, which means that any variable can hold any type of value. Since variables can change their type, the Javascript standard provides developers with a special operator called typeof that returns what type of value a variable holds.

Javascript data types can be either simple or complex, ranging from a simple integer to a complex object, and encompass a wider range than data types in JSON. The list of possible data types the typeof operator can identify has expanded over time, setting the stage for more complex data type interactions in the process.

This article examines the different Javascript data types, including those added in the most recent versions of Javascript. Then, we explain why data types matter and how the Javascript typeof operator keeps developers from making data typing mistakes.

Basics of Javascript Data Types

Javascript 2020 (also known as ECMAScript or ES11) defines nine different data types. Two new Javascript data types — Symbol and BigInt — have been added to this list since 2015, so older Javascript code will not work properly with code that uses these new data types.

The nine Javascript data types are:

  • Null — Used when an object has no value.
  • Undefined — Used when an object has a value, but the value is not defined.
  • Boolean — True or false value.
  • Number — Integer or floating point (decimal) number.
  • String — Sequence of characters that represent text.
  • Object — Any complex data structure. Javascript arrays also fall into this category.
  • Function — A Javascript function.
  • Symbol — Anonymous, unique identifier. (Added in 2015.)
  • BigInt — Special type of number that can be larger than the maximum value specified by the Number type and whose precision is only limited by computer memory. (Added in 2020.)

Javascript classes, which are essential to modern object-oriented programming on the web, are not distinct data types; they are a type of Object. Conceptually, classes and objects within modern Javascript now have different meanings, so it’s important to remember that the type for a class is still Object.

Developers distinguish each of these data types using the typeof operator. For customized data types, which would always have a base data type of Object, the instanceof operator provides more data about their specific type names. This includes objects built into the Javascript language, like Set or Date.

Why Javascript Data Types Matter

Each Javascript data type has a specific purpose in the language, and the same value in a different data type may not provide the same functionality or return the same result. For example, adding a number to a string usually results in a concatenated string, while adding two numbers together results in a larger number:

let example1 = "1" + 2;  // the text string "12"
let example2 = 1 + 2;    // the number 3

Unexpected behavior like this is common when combining data types, so it is important to make sure that all data are the same type before trying to combine them. In the previous example, to force the first expression to result in a number, you must convert it to a number first:

let example3a = "1";
let example3b = parseInt(example3a); // cast the string to a number
let example3c = example3b + 2;       // the number 3

Developers must also remember that even though some Javascript data types have some functional overlap, they are not interchangeable. For example, there is a distinct difference between the null and undefined types. Some Javascript methods may return one or both of these in different contexts.

let example4a;
let example4b = null;    // variable has a "null" value
console.log(example4a);  // printing undefined variables is unhelpful
console.log(example4b);  // printing null variables is just fine

Using the Javascript Typeof Operator

The Javascript typeof operator returns a string that developers evaluate with strict comparison to identify a value’s base data type. Developers can rely on these strings being identical in any modern Javascript implementation, as listed below:

  • Null — typeof instance === “object”
  • Undefined — typeof instance === “undefined”
  • Boolean — typeof instance === “boolean”
  • Number — typeof instance === “number”
  • String — typeof instance === “string”
  • Object — typeof instance === “object”
  • Function — typeof instance === “function”
  • Symbol — typeof instance === “symbol”
  • BigInt — typeof instance === “bigint”

The null type has returned “object” as its type since the earliest days of Javascript. There has been at least one proposal over the last decade to change its typeof response to “null,” but the idea has never been implemented. Reasons range from legacy code compatibility to this inconsistency just not being a major issue, just a small quirk developers need to remember.


Datatypes are a vital part of the Javascript language, even though Javascript is a dynamic language. Managing data types properly prevents unexpected behavior in programs, and switching between data types within the same variable is risky if you don’t track the variable’s contents well.

The Javascript typeof operator is the most reliable way to track changes in Javascript data types, especially those in newer versions of Javascript. Remember that the typeof operator cannot detect customized data types or object names, only those built into the language.

Enroll in our Intro to Programming Nanodegree Program today to learn more about Javascript data types, the Javascript typeof operator, and other programming concepts.

Start Learning

Stay updated on the latest in technology

"*" indicates required fields

Stay updated on the Latest in Tech

Be first to hear about special offers, news, and resources from Udacity

"*" indicates required fields