JavaScript: Data Types

By Xah Lee. Date: . Last updated: .

Variable doesn't have type. Value has type.

JavaScript data types
type nameexplanation
undefinedundefined is the only value of this type. 〔►see JavaScript: undefined
nullnull is the only value of this type. 〔►see JavaScript: null
† booleantrue and false are the only possible values. 〔►see JavaScript: true, false
† string 〔►see JavaScript: String Object
† numberIncludes special value NaN (Not A Number) and Infinity 〔►see JavaScript: NaN〕 〔►see JavaScript: Infinity
† symbol Always-unique value primarily for use as property keys. 〔►see JavaScript: Symbol Tutorial
objectThe “object” data type is a collection of key/value pairs. Array, Function, Date, Regex, … are all specialized objects. 〔►see JavaScript: What's Object?

† A value of these types are called primitive values.

〔►see JavaScript: Primitive Value

ECMAScript 2015 §ECMAScript Data Types and Values#sec-ecmascript-data-types-and-values

Find the Type of a Value

Use typeof value to check a value's type.

// following are all true
typeof undefined === "undefined";

typeof "abc" === "string";

typeof true === "boolean";
typeof false === "boolean";

typeof 3 === "number";
typeof NaN === "number";
typeof Infinity === "number"
// type of some standard objects
// following are all true

console.log( typeof {} === "object" );

console.log( typeof [3,4] === "object" );

console.log( typeof (new Date()) === "object" );

console.log( typeof /x/ === "object" ); // this is regex object

console.log( typeof JSON === "object" );

console.log( typeof Math === "object" );

〔►see JavaScript Objects Reference Panel Index

typeof undefined

undefined is the value of un-assigned variable, non-existent property access, function without return statement, out-of-bound array access, etc.

〔►see JavaScript: undefined

typeof null

typeof null returns "object". This is historical bug and we are stuck with it. (it should return "null")

〔►see JavaScript: null

typeof function

typeof function returns "function". This is really useful, but is a JavaScript quirk, because, by spec, JavaScript doesn't have datatype “function”.

console.log(
    typeof (function () {}) === "function"
); // true

The data type of JavaScript function actually is object, by spec.

ECMAScript® 2016 Language Specification#sec-terms-and-definitions-function

〔►see JavaScript: What's Object?

Object Type

Object is the most important type in JavaScript.

〔►see JavaScript: Object System Overview

Determine Type of Object

〔►see JavaScript: Determine Type of Object

“typeof” is a Operator

Note: typeof is a operator, not a function. This means, you can write typeof 3. Write typeof (expr) when expr is complex.

ECMAScript 2015 §ECMAScript Language: Expressions#sec-typeof-operator

JavaScript Basics

  1. How to Run JavaScript
  2. JavaScript: How to Use Browser's JS Console
  3. JavaScript Tutorial by Example
  4. JavaScript: Data Types
  5. JavaScript: true, false
  6. JavaScript: Operators
  7. JavaScript: Variable
  8. JavaScript: Branch Control: if then else, switch
  9. JavaScript: for while do Loop
  10. JavaScript: Array Basics
  11. JavaScript: Object Basics
  12. JavaScript: RegExp Tutorial
  13. JavaScript: Throw Try Catch Finally
Like what you read? Buy JavaScript in Depth
or, buy a new keyboard, see Keyboard Reviews.