JavaScript typeof

JavaScript typeof

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

typeof operator

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

Example

typeof "Bill"                 // Returns "string"
typeof 3.14                   // Returns "number"
typeof NaN                    // Returns "number"
typeof false                  // Returns "boolean"
typeof [1,2,3,4]              // Returns "object"
typeof {name:'Bill', age:19}  // Returns "object"
typeof new Date()             // Returns "object"
typeof function () {}         // Returns "function"
typeof myCar                  // Returns "undefined" *
typeof null                   // Returns "object"

Try It Yourself

Note that:

  • The data type of NaN is number
  • The data type of array is object
  • The data type of date is object
  • The data type of null is object
  • The data type of an undefined variable is undefined *
  • The data type of an uninitialized variable is also undefined *

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

Original date

Primitive data value refers to a single simple data value without any additional properties or methods.

typeof The operator can return one of the following primitive types:

  • string
  • number
  • boolean
  • undefined

Example

typeof "Bill"              // Returns "string"
typeof 3.14                // Returns "number"
typeof true                // Returns "boolean"
typeof false               // Returns "boolean"
typeof x                   // Returns "undefined" (if x has no value)

Try It Yourself

Complex data

typeof The operator can return one of two complex types:

  • function
  • object

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

typeof The operator will not return "object" for a function.

Example

typeof {name:'Bill', age:19} // Returns "object"
typeof [1,2,3,4]             // Returns "object" (not "array", please note the example below)
typeof null                  // Returns "object"
typeof function myFunc(){}   // Returns "function"

Try It Yourself

typeof The operator will return for an array "object", because in JavaScript, an array is an object.

Data types of typeof

typeof The operator is not a variable. It is just an operator. The operator (+ - * /) has no data type.

However,typeof The operator is alwaysReturns a string(including the type of the operand).

constructor property

constructor The property returns the constructor of all JavaScript variables.

Example

"Bill".constructor                // Returns function String()  {[native code]}
(3.14).constructor                // Returns function Number()  {[native code]}
false.constructor                 // Returns function Boolean() {[native code]}
[1,2,3,4].constructor             // Returns function Array()[native code]
{name:'Bill',age:19}.constructor  // Returns function Object()[native code]
new Date().constructor            // Returns function Date()[native code]
function () {}.constructor        // Returns function Function()[native code]

Try It Yourself

You can check the constructor property to determine if the object is an array (including "Array" word):

Example

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

Try It Yourself

or, more simply, you can check if the object isarray function:

Example

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

Try It Yourself

You can check the constructor property to determine if the object is a date (including "Date" word):

Example

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

Try It Yourself

or, more simply, you can check if the object isdate function:

Example

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

Try It Yourself

Undefined

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

Example

let car;    // The value is undefined, and the type is also undefined.

Try It Yourself

by setting its value to undefinedcan clear any variable. The type will also be undefined.

Example

car = undefined;    // The value is undefined, and the type is also undefined.

Try It Yourself

An empty value

An empty value and undefined irrelevant.

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

Example

let car = "";    // The value is "", and the type is "string"

Try It Yourself

Null

In JavaScript, null means 'no'. It should be something that does not exist.

Unfortunately, in JavaScript,null The data type is an object.

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

You can set an object to null To clear an object:

Example

let person = {firstName:"Bill", lastName:"Gates", age:19, eyeColor:"blue"};
person = null;    // Now the value is null, but the type is still object

Try It Yourself

You can also clear an object by setting it to undefined To clear an object:

Example

let person = {firstName:"Bill", lastName:"Gates", age:19, eyeColor:"blue"};
person = undefined;   // Now both the value and type are undefined

Try It Yourself

Difference between undefined and null

undefined and null Values are equal but types are different:

typeof undefined           // undefined
typeof null                // object
null === undefined         // false
null == undefined          // true

Try It Yourself