Professional Documents
Culture Documents
New 1
New 1
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. Wherever possible, comparisons with other languages
are drawn.
Dynamic typing
JavaScript is a loosely typed and dynamic language. Variables in JavaScript are not
directly associated with any particular value type, and any variable can be
assigned (and re-assigned) values of all types:
Primitive values (immutable datum represented directly at the lowest level of the
language)
Boolean type
Null type
Undefined type
Number type
BigInt type
String type
Symbol type
Objects (collections of properties)
Primitive values
All types except objects define immutable values (that is, values which can't be
changed). For example, Strings are immutable. We refer to values of these types as
"primitive values".
Boolean type
Boolean represents a logical entity and can have two values: true and false. See
Boolean and Boolean for more details.
Null type
The Null type has exactly one value: null. See null and Null for more details.
Undefined type
A variable that has not been assigned a value has the value undefined. See
undefined and Undefined for more details.
Numeric types
ECMAScript has two built-in numeric types: Number and BigInt — along with the
related value NaN.
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 2^1024 (Number.MAX_VALUE) as well as negative floating-point
numbers between -(2^-1074) and -(2^1024), but it can only safely store integers in
the range -(2^53 − 1) (Number.MIN_SAFE_INTEGER) to 2^53 − 1
(Number.MAX_SAFE_INTEGER).
Note: You can check if a number is within the range of safe integers using
Number.isSafeInteger(). Outside the range from Number.MIN_SAFE_INTEGER to
Number.MAX_SAFE_INTEGER, JavaScript can no longer safely represent integers; they
will instead be represented by a double-precision floating point approximation.
The Number type has only one integer with multiple representations: 0 is
represented as both -0 and +0 (where 0 is an alias for +0). In practice, there is
almost no difference between the different representations; for example, +0 === -0
is true. However, you are able to notice this when you divide by zero:
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.
BigInt type
The BigInt type is a numeric primitive in JavaScript that can represent integers
with arbitrary precision. With BigInts, you can safely store and operate on large
integers even beyond the safe integer limit for Numbers.
You can obtain the largest safe value that can be incremented with Numbers by using
the constant Number.MAX_SAFE_INTEGER. With the introduction of BigInts, you can
operate with numbers beyond the Number.MAX_SAFE_INTEGER.
// 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
Copy to Clipboard
You can use the operators +, *, -, **, and % with BigInts—just like with Numbers. A
BigInt is not strictly equal to a Number, but it is loosely so.
A BigInt behaves like a Number in cases where it is converted to boolean: if, ||,
&&, Boolean, !.
NaN
NaN ("Not a Number") is 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.
String type
JavaScript's String type is used to represent textual data. It is a set of
"elements" of 16-bit unsigned integer values. Each element in the String occupies a
position in the String. The first element is at index 0, the next at index 1, and
so on. The length of a String is the number of elements in it.
JavaScript strings are immutable. This means that once a string is created, it is
not possible to modify it.
Use strings for textual data. When representing complex data, parse strings, and
use the appropriate abstraction.
Symbol type
A Symbol is a unique and immutable primitive value and may be used as the key of an
Object property (see below). In some programming languages, Symbols are called
"atoms".
Objects
In computer science, an object is a value in memory which is possibly referenced by
an identifier.
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. Property values can be values of any type, including other
objects, which enables building complex data structures. Properties are identified
using key values. A key value is either a String value or a Symbol value.
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 changed with an assignment.
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 an
accessor property, and can have its attributes changed.
Accessor property
Associates a key with one of two accessor functions (get and set) to retrieve or
store a value.
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.
get
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.
Functions are regular objects with the additional capability of being callable.
Dates
When representing dates, the best choice is to use the built-in Date utility in
JavaScript.
Typed Arrays present an array-like view of an underlying binary data buffer, and
offer many methods that have similar semantics to the array counterparts. "Typed
array" is an umbrella term for a range of data structures, including Int8Array,
Float32Array, etc. Check the typed array page for more information.
The difference between Maps and WeakMaps is that in the former, object keys can be
enumerated over. This allows garbage collection optimizations in the latter case.
You could implement Maps and Sets yourself. However, since objects cannot be
compared (in the sense of < "less than", for instance), neither does the engine
expose its hash function for objects, look-up performance would necessarily be
linear. Native implementations of them (including WeakMaps) can have look-up
performance that is approximately logarithmic to constant time.
Usually, to bind data to a DOM node, one could set properties directly on the
object, or use data-* attributes. This has the downside that the data is available
to any script running in the same context. Maps and WeakMaps make it easy to
privately bind data to an object.
Please have a look at the reference to find out about more objects.
Determining types using the typeof operator
The typeof operator can help you to find the type of your variable.
Please read the reference page for more details and edge cases.
See also
JavaScript Data Structures and Algorithms by Oleksii Trekhleb
Nicholas Zakas collection of common data structure and common algorithms in
JavaScript.
Search Tre(i)es implemented in JavaScript
Data Types and Values in the ECMAScript specification
Found a problem with this page?
Edit on GitHub
Source on GitHub
Report a problem with this content on GitHub
Want to fix the problem yourself? See our Contribution guide.
Last modified: Aug 12, 2022, by MDN contributors