1 - JavaScript Data Types and Data Structures - JavaScript - MDN
1 - JavaScript Data Types and Data Structures - JavaScript - MDN
Programming languages all have built-in data structures, but these often differ from one
language to another. This article attempts to list the built-in data structures available in
JavaScript and what properties they have. These can be used to build other data
structures.
The language overview offers a similar summary of the common data types, but with more
comparisons to other languages.
JavaScript is also a weakly typed language, which means it allows implicit type
conversion when an operation involves mismatched types, instead of throwing type errors.
JS
Implicit coercions are very convenient, but can create subtle bugs when conversions
happen where they are not expected, or where they are expected to happen in the other
direction (for example, string to number instead of number to string). For symbols and
BigInts, JavaScript has intentionally disallowed certain implicit type conversions.
Primitive values
All types except Object define immutable values represented directly at the lowest level of
the language. We refer to values of these types as primitive values.
All primitive types, except null , can be tested by the typeof operator. typeof null returns
"object" , so one has to use === null to test for null .
All primitive types, except null and undefined , have their corresponding object wrapper
types, which provide useful methods for working with the primitive values. For example,
the Number object provides methods like toExponential() . When a property is accessed on
a primitive value, JavaScript automatically wraps the value into the corresponding wrapper
object and accesses the property on the object instead. However, accessing a property on
null or undefined throws a TypeError exception, which necessitates the introduction of
The object wrapper classes' reference pages contain more information about the methods
and properties available for each type, as well as detailed descriptions for the semantics
of the primitive types themselves.
Null type
The Null type is inhabited by exactly one value: null .
Undefined type
The Undefined type is inhabited by exactly one value: undefined .
Conceptually, undefined indicates the absence of a value, while null indicates the
absence of an object (which could also make up an excuse for typeof null === "object" ).
The language usually defaults to undefined when something is devoid of a value:
A return statement with no value ( return; ) implicitly returns undefined .
Accessing a nonexistent object property ( obj.iDontExist ) returns undefined .
A variable declaration without initialization ( let x; ) implicitly initializes the variable to
undefined .
null is used much less often in the core language. The most important place is the end of
the prototype chain — subsequently, methods that interact with prototypes, such as
Object.getPrototypeOf() , Object.create() , etc., accept or return null instead of undefined .
Boolean values are usually used for conditional operations, including ternary operators,
if...else , while , etc.
Number type
The Number type is a double-precision 64-bit binary format IEEE 754 value. It is capable of
storing positive floating-point numbers between 2-1074 ( Number.MIN_VALUE ) and 21024
( Number.MAX_VALUE ) as well as negative floating-point numbers between -2-1074 and -21024,
but it can only safely store integers in the range -(253 − 1) ( Number.MIN_SAFE_INTEGER ) to 253
− 1 ( Number.MAX_SAFE_INTEGER ). Outside this range, JavaScript can no longer safely represent
integers; they will instead be represented by a double-precision floating point
approximation. You can check if a number is within the range of safe integers using
Number.isSafeInteger() .
NaN ("Not a Number") is a special kind of number value that's typically encountered when
the result of an arithmetic operation cannot be expressed as a number. It is also the only
value in JavaScript that is not equal to itself.
Although a number is conceptually a "mathematical value" and is always implicitly floating-
point-encoded, JavaScript provides bitwise operators. When applying bitwise operators,
the number is first converted to a 32-bit integer.
Note: Although bitwise operators can be used to represent several Boolean
values within a single number using bit masking , this is usually considered a
bad practice. JavaScript offers other means to represent a set of Booleans (like
an array of Booleans, or an object with Boolean values assigned to named
properties). Bit masking also tends to make the code more difficult to read,
understand, and maintain.
It may be necessary to use such techniques in very constrained environments, like when
trying to cope with the limitations of local storage, or in extreme cases (such as when each
bit over the network counts). This technique should only be considered when it is the last
measure that can be taken to optimize size.
BigInt type
The BigInt type is a numeric primitive in JavaScript that can represent integers with
arbitrary magnitude. With BigInts, you can safely store and operate on large integers even
beyond the safe integer limit ( Number.MAX_SAFE_INTEGER ) for Numbers.
A BigInt is created by appending n to the end of an integer or by calling the BigInt()
function.
This example demonstrates where incrementing the Number.MAX_SAFE_INTEGER returns the
expected result:
JS
// BigInt
const x = BigInt(Number.MAX_SAFE_INTEGER); // 9007199254740991n
x + 1n === x + 2n; // false because 9007199254740992n and 9007199254740993n are unequal
// Number
Number.MAX_SAFE_INTEGER + 1 === Number.MAX_SAFE_INTEGER + 2; // true because both are
9007199254740992
You can use most operators to work with BigInts, including + , * , - , ** , and % — the only
forbidden one is >>> . A BigInt is not strictly equal to a Number with the same
mathematical value, but it is loosely so.
BigInt values are neither always more precise nor always less precise than numbers, since
BigInts cannot represent fractional numbers, but can represent big integers more
accurately. Neither type entails the other, and they are not mutually substitutable. A
TypeError is thrown if BigInt values are mixed with regular numbers in arithmetic
Objects
In computer science, an object is a value in memory which is possibly referenced by an
identifier. In JavaScript, objects are the only mutable values. Functions are, in fact, also
objects with the additional capability of being callable.
Properties
In JavaScript, objects can be seen as a collection of properties. With the object literal
syntax, a limited set of properties are initialized; then properties can be added and
removed. Object properties are equivalent to key-value pairs. Property keys are either
strings or symbols. Property values can be values of any type, including other objects,
which enables building complex data structures.
There are two types of object properties: The data property and the accessor property.
Each property has corresponding attributes. Each attribute is accessed internally by the
JavaScript engine, but you can set them through Object.defineProperty() , or read them
through Object.getOwnPropertyDescriptor() . You can read more about the various nuances
on the Object.defineProperty() page.
Data property
Data properties associate a key with a value. It can be described by the following
attributes:
value
The value retrieved by a get access of the property. Can be any JavaScript value.
writable
A boolean value indicating if the property can be enumerated by a for...in loop. See
also Enumerability and ownership of properties for how enumerability interacts with
other functions and syntaxes.
configurable
Note: It's important to recognize it's accessor property — not accessor method.
We can give a JavaScript object class-like accessors by using a function as a
value — but that doesn't make the object a class.
A function called with an empty argument list to retrieve the property value whenever a
get access to the value is performed. See also getters. May be undefined .
set
A function called with an argument that contains the assigned value. Executed whenever
a specified property is attempted to be changed. See also setters. May be undefined .
enumerable
A boolean value indicating if the property can be enumerated by a for...in loop. See
also Enumerability and ownership of properties for how enumerability interacts with
other functions and syntaxes.
configurable
A boolean value indicating if the property can be deleted, can be changed to a data
property, and can have its attributes changed.
The prototype of an object points to another object or to null — it's conceptually a hidden
property of the object, commonly represented as [[Prototype]] . Properties of the object's
[[Prototype]] can also be accessed on the object itself.
Objects are ad-hoc key-value pairs, so they are often used as maps. However, there can
be ergonomics, security, and performance issues. Use a Map for storing arbitrary data
instead. The Map reference contains a more detailed discussion of the pros & cons
between plain objects and maps for storing key-value associations.
Dates
When representing dates, the best choice is to use the built-in Date utility in JavaScript.
Indexed collections: Arrays and typed Arrays
Arrays are regular objects for which there is a particular relationship between integer-
keyed properties and the length property.
Additionally, arrays inherit from Array.prototype , which provides a handful of convenient
methods to manipulate arrays. For example, indexOf() searches a value in the array and
push() appends an element to the array. This makes Arrays a perfect candidate to
Type coercion
As mentioned above, JavaScript is a weakly typed language. This means that you can
often use a value of one type where another type is expected, and the language will
convert it to the right type for you. To do so, JavaScript defines a handful of coercion
rules.
Primitive coercion
The primitive coercion process is used where a primitive value is expected, but there's
no strong preference for what the actual type should be. This is usually when a string, a
number, or a BigInt are equally acceptable. For example:
The Date() constructor, when it receives one argument that's not a Date instance —
strings represent date strings, while numbers represent timestamps.
The + operator — if one operand is a string, string concatenation is performed;
otherwise, numeric addition is performed.
The == operator — if one operand is a primitive while the other is an object, the object
is converted to a primitive value with no preferred type.
This operation does not do any conversion if the value is already a primitive. Objects are
converted to primitives by calling its [@@toPrimitive]() (with "default" as hint), valueOf() ,
and toString() methods, in that order. Note that primitive conversion calls valueOf()
before toString() , which is similar to the behavior of number coercion but different from
string coercion.
The [@@toPrimitive]() method, if present, must return a primitive — returning an object
results in a TypeError . For valueOf() and toString() , if one returns an object, the return
value is ignored and the other's return value is used instead; if neither is present, or
neither returns a primitive, a TypeError is thrown. For example, in the following code:
JS
Object]" .
The [@@toPrimitive]() method always takes precedence when doing conversion to any
primitive type. Primitive conversion generally behaves like number conversion, because
valueOf() is called in priority; however, objects with custom [@@toPrimitive]() methods
can choose to return any primitive. Date and Symbol objects are the only built-in objects
that override the [@@toPrimitive]() method. Date.prototype[@@toPrimitive]() treats the
"default" hint as if it's "string" , while Symbol.prototype[@@toPrimitive]() ignores the hint
See also
JavaScript Data Structures and Algorithms by Oleksii Trekhleb
Computer Science in JavaScript by Nicholas C. Zakas
This page was last modified on Dec 19, 2023 by MDN contributors.