Cover image for JavaScript Data Types in a Nutshell

JavaScript Data Types in a Nutshell

naveens16 profile image Naveen.S ・3 min read


Every value in the JavaScript language belongs to a certain data type. There are six types of JavaScript data. (ES6 has added a seventh symbol type value, which is not covered in this tutorial.)

  • Number (number): integer and decimal (such as 1and 3.14)
  • String: text (for example Hello World).
  • Boolean (boolean): two special values ​​that represent authenticity, namely true (true) and false (false)
  • undefined: Indicates "undefined" or does not exist, that is, because there is currently no definition, there is no value here
  • null: Indicates a null value, that is, the value here is null.
  • Object: A collection of various values.

In general, the three types of numeric, string, and Boolean values ​​are collectively called primitive type values, that is, they are the most basic data types and cannot be subdivided. Objects are called complex type values, because an object is often a combination of multiple primitive type values, which can be seen as a container for storing various values. As for the undefined sum null, they are generally regarded as two special values.

Objects are the most complex data type and can be divided into three subtypes.

  • Narrow object (object)
  • Array
  • Function

Narrowly defined objects and arrays are two different ways of combining data. Unless otherwise stated, the "objects" in this tutorial refer specifically to narrowly defined objects. A function is actually a method of processing data. JavaScript treats it as a data type that can be assigned to variables, which brings great flexibility to programming and lays the foundation for JavaScript's "functional programming".

Types of Operator

JavaScript has three methods to determine what type a value is.

  • typeof Operator
  • instanceof Operator
  • Object.prototype.toString method

instanceof Operators and Object.prototype.toString methods will be introduced later. typeof Operators are introduced here.

typeof The operator can return the data type of a value.

Numbers, strings, Boolean values are returned number, string, boolean.

typeof 123 // "number"
typeof '123' // "string"
typeof false // "boolean"

The function returns function.

function f() {}
typeof f
// "function"

undefined Return undefined.

typeof undefined
// "undefined"

Using this, it typeof can be used to check an undeclared variable without reporting an error.

// ReferenceError: v is not defined

typeof v
// "undefined"

In the above code, the variable is v not var declared with a command, and it will report an error if it is used directly. However, if you put it in the typeof back, you will not report an error but return undefined.

In actual programming, this feature is usually used to judge sentences.

// Wrong writing
if (v) {
  // ...
// ReferenceError: v is not defined

// correct writing
if (typeof v === "undefined") {
  // ...

The object returns object.

typeof window // "object"
typeof {} // "object"
typeof [] // "object"

In the above code, []the type of the empty array ( ) is the same object, which means that within JavaScript, an array is essentially a special object. By the way, the instanceof operator can distinguish between arrays and objects. instanceof.

null return object

typeof null // "object"

null The type is object that this is due to historical reasons. In the first version of the JavaScript language in 1995, only five data types (objects, integers, floating-point numbers, strings, and boolean values) were designed. It was not considered null, but only regarded as object a special value. Later null, as an independent data type, in order to be compatible with the previous code, the typeof null return object cannot be changed.

Posted on by:

naveens16 profile



I'm the Founder and Front-End Engineer of Themesfinity. I help clients to build stunning websites and web apps to solve their business problems using JavaScript ecosystem (React, Nodejs & Nextjs)


markdown guide