Codectionary / Developer documentation / JavaScript

Data Types

JavaScript has seven primitive data types - string, number, boolean, undefined, null, bigint, and symbol - plus the object type, which includes arrays, functions, and plain objects. JavaScript is dynamically typed, meaning a variable can hold any type and that type can change. The typeof operator reports a value's type at runtime.

Syntax

let value = "text"; // string, number, boolean, etc.
typeof value; // "string"

Examples

The Primitive Types

Examples of each primitive type and what typeof reports for it.

typeof "hello";        // "string"
typeof 42;              // "number"
typeof 3.14;            // "number" - no separate float type
typeof true;            // "boolean"
typeof undefined;       // "undefined"
typeof null;            // "object" - a famous long-standing JS quirk
typeof 10n;             // "bigint"
typeof Symbol("id");    // "symbol"
typeof { a: 1 };        // "object"
typeof [1, 2, 3];       // "object" - arrays are objects too
typeof function(){};    // "function"

Dynamic Typing

A variable's type can change as new values are assigned to it.

let value = "hello"; // string
value = 42;           // now a number
value = true;         // now a boolean
value = { a: 1 };     // now an object
// No errors - JavaScript allows this by design

Best practices

  • Use typeof to check a value's type at runtime, but remember typeof null is "object" due to a long-standing language quirk
  • Use Array.isArray() rather than typeof to check specifically for arrays, since typeof reports both as "object"
  • Prefer === over == to avoid unexpected type coercion during comparisons
  • Consider TypeScript if your project would benefit from catching type mismatches before runtime

At a glance

Purpose
Application logic and interaction
File extension
.js ยท .mjs
Runs in
Browsers and JavaScript runtimes
Usually used with
HTML, CSS and Web APIs

Specifications & further reading

Related JavaScript documentation