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: What's “null” Used For?
booleantrue and false are the only possible values. 〔►see JavaScript: What's True, False?
string 〔►see JavaScript: String Object
numberIncludes special value NaN (Not A Number) and Infinity 〔►see JavaScript: NaN〕 〔►see JavaScript: Infinity
objectThe “object” data type is a collection of key/value pairs. Array, Function, Date, Regex, … are all specialized objects. 〔►see JavaScript: What's Object?
symbol Always-unique value primarily for use as property keys. 〔►see JavaScript: Symbol Tutorial

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

Primitive Value

The following values are called “Primitive Value”.

ECMAScript 2015 §Overview#sec-primitive-value

For {string, number, boolean} types, they have a corresponding object type. The object is used as primitive value wrappers. This allows you to call methods on them, such as "abc".length or (3.44).toString().

〔►see JavaScript: Primitive Value Object Wrapper

Find the Type of a Value

Use typeof value to check a value's type.

typeof undefined === "undefined";
// true
typeof "abc" === "string";
// true
typeof false === "boolean";
// true
// all true
typeof 3 === "number";
typeof 3.5 === "number";
typeof NaN === "number";
typeof Infinity === "number"
// type of some standard objects
console.log(
    typeof {} === "object"
); // true

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

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

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

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

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

Data Type “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.

〔►see JavaScript: What's “null” Used For?

typeof function

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

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

The data type of JavaScript function actually is object, by definition, it's a collection of key/value pairs. 〔►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: What's 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.