Suggest an editImprove this articleRefine the answer for “Data types in JavaScript”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)**JavaScript has eight data types: seven primitives (Number, BigInt, String, Boolean, Null, Undefined, Symbol) and one reference type, Object.** A primitive is a value with no methods of its own that is copied by value; an Object holds collections of data and functions and is passed by reference. Arrays, functions, `Date`, `Map` and `Set` are not separate types, they are flavours of Object. ```javascript typeof 42; // 'number' typeof 10n; // 'bigint' typeof Symbol('id'); // 'symbol' typeof null; // 'object' - a historical language bug typeof [1, 2, 3]; // 'object' ``` **Key point:** seven primitives plus Object, eight in total; primitives are copied by value, objects by reference.Shown above the full answer for quick recall.Answer (EN)Image**JavaScript has eight basic data types**, split into primitives (seven of them) and the reference, object type (one). A primitive is a value that is not an object and has no methods of its own, while an object is a container for data and functions that is passed by reference. ## Theory ### TL;DR - There are exactly 8 types: `Number`, `BigInt`, `String`, `Boolean`, `Null`, `Undefined`, `Symbol` and `Object`. - The first seven are primitives: stored by value, with no methods of their own. - `Object` is the only reference type; `Array`, `Function`, `Date`, `RegExp`, `Map` and `Set` are flavours of it. - Every ordinary number is a 64-bit floating point number (IEEE 754), hence `Infinity`, `-Infinity` and `NaN`. - `typeof null` returns `'object'`, an old language bug kept for backwards compatibility. ### Quick example ```javascript const count = 42; // Number const huge = 123456789012345678n; // BigInt const name = 'Tim'; // String const isAdmin = true; // Boolean const nothing = null; // Null let notAssigned; // Undefined const id = Symbol('id'); // Symbol const user = { name, [id]: 1 }; // Object console.log(typeof count, typeof huge, typeof name); // number bigint string console.log(typeof nothing, typeof notAssigned, typeof id); // object undefined symbol ``` ### Primitives versus objects A primitive is a simple immutable value. When you assign it to another variable the value itself is copied, so the two variables live independently. An object, by contrast, sits in memory on its own and the variable holds only a reference to it, so the reference is copied, not the contents. ```javascript let a = 10; let b = a; b = 20; console.log(a); // 10, the primitive was copied by value const first = { n: 10 }; const second = first; second.n = 20; console.log(first.n); // 20, both variables look at the same object ``` Primitives have no methods, yet `'abc'.toUpperCase()` works: the engine creates a temporary wrapper object on the fly, takes the method from it and throws the wrapper away immediately. ### Numeric types: Number and BigInt #### Number Numbers, both integers and floating point ones. ```javascript let a = 42; let b = 3.14; ``` Details: - There are special values: `Infinity`, `-Infinity`, `NaN` (Not-a-Number). - Every number in JavaScript is a 64-bit floating point number (IEEE 754), which is why `0.1 + 0.2 !== 0.3`. - The safe integer range is capped by `Number.MAX_SAFE_INTEGER` (2 to the power of 53 minus 1). #### BigInt Used for very large integers that fall outside the range of `Number`. ```javascript let big = 123456789012345678901234567890n; ``` > The `n` suffix is added. It lets you work precisely with integers of any length. `BigInt` does not mix with `Number` in arithmetic: `1n + 1` throws a `TypeError`, an explicit conversion is required first. ### Text and logic: String and Boolean #### String Lines of text in quotes: single, double or template ones. ```javascript let name = 'Tim'; let message = `Hello, ${name}!`; ``` > Strings are immutable: a "change" creates a new string and leaves the old one untouched. ```javascript let s = 'abc'; s[0] = 'X'; console.log(s); // 'abc', the assignment is simply ignored ``` #### Boolean Logical values: `true` or `false`. ```javascript let isAdmin = true; let isLoggedIn = false; ``` > Used in conditions, checks and logical expressions. The only falsy values are `false`, `0`, `-0`, `0n`, `''`, `null`, `undefined` and `NaN`; everything else is truthy. ### Empty values and Symbol #### Null A special value meaning "nothing" or "empty", assigned deliberately by the programmer. ```javascript let user = null; ``` > `typeof null` returns `'object'`, a historical JavaScript bug that was never fixed. #### Undefined A variable is declared but holds no value. ```javascript let x; console.log(x); // undefined ``` > It is assigned automatically when no value is given: it is also what you read from a missing property and what an omitted parameter holds. #### Symbol A unique, immutable value, often used as an object key. ```javascript let id = Symbol('id'); let obj = { [id]: 123 }; console.log(Symbol('id') === Symbol('id')); // false ``` > Every Symbol is unique even when the description is the same. Symbol keys do not show up in `Object.keys()` or `JSON.stringify()`, which makes them suitable for internal fields. ### Object: the reference type Holds collections of data and functions. Passed by reference, not by value. ```javascript let user = { name: 'Tim', age: 25 }; let arr = [1, 2, 3]; let func = function () {}; ``` Flavours of object: - `Object`, the base type. - `Array`, an array. - `Function`, a function. - `Date`, `RegExp`, `Map`, `Set` and so on. | Data type | Example | Description | | --- | --- | --- | | **Number** | `42`, `3.14`, `NaN` | Numbers | | **BigInt** | `123n` | Large integers | | **String** | `'Hello'`, `` `Hi ${name}` `` | Strings | | **Boolean** | `true`, `false` | Logical type | | **Null** | `null` | Absence of a value | | **Undefined** | `undefined` | Not defined | | **Symbol** | `Symbol('id')` | Unique identifier | | **Object** | `{}`, `[]`, `function(){}` | Collections and structures | ### Common mistakes - **Calling an array a separate type.** `typeof [1, 2]` gives `'object'`; you need `Array.isArray()` to tell an array apart. - **Mixing up `null` and `undefined`.** `undefined` means "the engine has not put a value here yet", `null` is emptiness the developer set on purpose. - **Forgetting that `typeof null === 'object'`** and building an object check on it: an extra `value !== null` condition is required. - **Comparing `NaN` with itself.** `NaN === NaN` is `false`, use `Number.isNaN()` instead. - **Mixing `BigInt` and `Number`** in one expression and getting a `TypeError`. - **Treating a string as mutable** and assigning a character by index; such a string has to be rebuilt from scratch.For the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.