JS: Data Types

By Xah Lee. Date: . Last updated: .

Variable doesn't have type. Value has type.

JavaScript data types
type namesample valueexplanation
† undefinedundefinedundefined is the only value of this type.
〔►see JS: undefined
† nullnullnull is the only value of this type.
〔►see JS: null
† booleantruetrue and false are the only possible values.
〔►see JS: true, false
† string"xyz" 〔►see JS: String Object
† number34Includes special value NaN (Not A Number) and Infinity
〔►see JS: NaN〕 〔►see JS: Infinity
† symbol Symbol() Always-unique value primarily for use as property keys. 〔►see JS: Symbol Tutorial
object{"x":3}The “object” data type is a collection of key/value pairs. Array, Function, Date, Regex, … are all specialized objects.
〔►see JS: What's Object?

† A value of these types are called primitive values.

〔►see JS: 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 JS: undefined

typeof null

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

〔►see JS: 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”.

    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 JS: What's Object?

Object Type

Object is the most important type in JavaScript.

〔►see JS: Object System Overview

Determine Type of Object

〔►see JS: 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. JS: How to Use Browser Console
  3. JavaScript Tutorial by Example
  4. JS: Data Types
  5. JS: true, false
  6. JS: Operators
  7. JS: Variable
  8. JS: Branch Control: if then else, switch
  9. JS: for while do Loop
  10. JS: Array Basics
  11. JS: Object Basics
  12. JS: RegExp Tutorial
  13. JS: Throw Try Catch Finally
Like what you read? Buy JavaScript in Depth