JS Data Structures
JS Data Structures
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. When possible,
comparisons with other languages are drawn.
Dynamic typing
JavaScript is a loosely typed or a 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:
var foo = 42; // foo is now a Number
var foo = 'bar'; // foo is now a String
var foo = true; // foo is now a Boolean
Data types
The latest ECMAScript standard defines seven data types:
Primitive values
All types except objects define immutable values (values, which are
incapable of being changed). For example and unlike to C, 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.
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 undefinedand Undefined for more details.
Number type
According to the ECMAScript standard, there is only one number type:
the double-precision 64-bit binary format IEEE 754 value (number
between -(253 -1) and 253 -1). There is no specific type for integers. In
addition to being able to represent floating-point numbers, the number
type has three symbolic values: +Infinity, -Infinity, and NaN(not-a-
number).
To check for the largest available value or smallest available
value within +/-Infinity, you can use the
constants Number.MAX_VALUE or Number.MIN_VALUE and starting with
ECMAScript 6, you are also able to check if a number is in the double-
precision floating-point number range using Number.isSafeInteger() as
well as Number.MAX_SAFE_INTEGER and Number.MIN_SAFE_INTEGER.
Beyond this range, integers in JavaScript are not safe anymore and will be
a double-precision floating point approximation of the value.
The number type has only one integer that has two representations: 0 is
represented as -0 and +0. ("0" is an alias for +0). In the praxis, this has
almost no impact. For example +0 === -0 is true. However, you are able
to notice this when you divide by zero:
> 42 / +0
Infinity
> 42 / -0
-Infinity
Although a number often represents only its value, JavaScript
provides some binary operators. These can be used to represent several
Boolean values within a single number using bit masking. However, this is
usually considered a bad practice, since 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 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 storage limitation of local storage or in
extreme cases 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.
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.
Unlike in languages like C, JavaScript strings are immutable. This means
that once a string is created, it is not possible to modify it. However, it is
still possible to create another string based on an operation on the
original string. For example:
Use strings for textual data. When representing complex data, parse
strings and use the appropriate abstraction.
Symbol type
Symbols are new to JavaScript in ECMAScript Edition 6. 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. For more details see Symboland the Symbol object
wrapper in JavaScript.
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 or a Symbol value.
There are two types of object properties which have certain attributes:
The data property and the accessor property.
Data property
Associates a key with one or two accessor functions (get and set) to
retrieve or store a value and has the following attributes:
Dates
When representing dates, the best choice is to use the built-
in Date utility in JavaScript.
TypedArray objects
Size
Value in Descriptio Web IDL Equivalen
Type
Range byte n type t C type
s
8-bit two's
Int8Array -128 to 127 1 complement byte int8_t
signed integer
8-bit unsigned
Uint8Array 0 to 255 1 octet uint8_t
integer
8-bit unsigned
Uint8ClampedArray 0 to 255 1 integer octet uint8_t
(clamped)
16-bit two's
-32768 to
Int16Array 2 complement short int16_t
32767
signed integer
16-bit unsigned unsigned
Uint16Array 0 to 65535 2 uint16_t
integer short
-
32-bit two's
2147483648
Int32Array 4 complement long int32_t
to
signed integer
2147483647
0 to 32-bit unsigned unsigned
Uint32Array 4 uint32_t
4294967295 integer long
32-bit
IEEE floating
1.2x10-38 t point number unrestrict
Float32Array 4 float
o 3.4x1038 ( 7 significant ed float
digits e.g.
1.1234567)
Float64Array 5.0x10-324 8 64-bit IEEE unrestrict double
to floating point ed double
1.8x10308 number (16
significant
digits e.g.
1.123...15)
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.