In JavaScript there are 5 different data types that can contain values:

string

number

boolean

object

function

There are 6 types of objects:

Object

Date

Array

String

Number

Boolean

And 2 data types that cannot contain values:

null

undefined


The typeof Operator

You can use the typeof operator to find the data type of a JavaScript variable.

Please observe:

The data type of NaN is number

The data type of an array is object

The data type of a date is object

The data type of null is object

The data type of an undefined variable is undefined *

The data type of a variable that has not been assigned a value is also undefined *

You cannot use typeof to determine if a JavaScript object is an array (or a date).

EXAMPLE ❯


Primitive Data

Primitive Data

A primitive data value is a single simple data value with no additional properties and methods.

The typeof operator can return one of these primitive types:

string

number

boolean

undefined

EXAMPLE ❯


Complex Data

The typeof operator can return one of two complex types:

function

object

The typeof operator returns "object" for objects, arrays, and null.

The typeof operator does not return "object" for functions.

The typeof operator returns "object" for arrays because in JavaScript arrays are objects.

EXAMPLE ❯


The Data Type of typeof

The typeofoperator is not a variable. It is an operator. Operators ( + - * / ) do not have any data type.

But, the typeof operator always returns a string (containing the type of the operand).


The constructor Property

The constructor property returns the constructor function for all JavaScript variables.

You can check the constructor property to find out if an object is an Array (contains the word "Array"):

function isArray(myArray) {
  return myArray.constructor.toString().indexOf("Array") > -1;
}

Or even simpler, you can check if the object is an Array function:

function isArray(myArray) {
  return myArray.constructor === Array;
}

You can check the constructor property to find out if an object is a Date (contains the word "Date"):

function isDate(myDate) {
  return myDate.constructor.toString().indexOf("Date") > -1;
}

Or even simpler, you can check if the object is a Date function:

function isDate(myDate) {
  return myDate.constructor === Date;
}

EXAMPLE ❯


Undefined

In JavaScript, a variable without a value, has the value undefined. The type is also undefined.

Any variable can be emptied, by setting the value to undefined. The type will also be undefined.

Example:

car = undefined;    // Value is undefined, type is undefined

EXAMPLE ❯


Empty Values

An empty value has nothing to do with undefined.

An empty string has both a legal value and a type.

EXAMPLE ❯


Null

In JavaScript null is "nothing". It is supposed to be something that doesn't exist.

Unfortunately, in JavaScript, the data type of null is an object.

You can consider it a bug in JavaScript that typeof null is an object. It should be null.

You can empty an object by setting it to null:

Example:

let person = {firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"};
person = null;    // Now value is null, but type is still an object

You can also empty an object by setting it to undefined:

EXAMPLE ❯


Difference Between Undefined and Null

undefined and null are equal in value but different in type:

EXAMPLE ❯