var foo = 42; // foo is now a Number var foo = "bar"; // foo is now a String var foo = true; // foo is now a Boolean
The latest ECMAScript standard defines seven data types:
- Six data types that are primitives:
- and Object
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 represents a logical entity and can have two values:
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:
To check for larger or smaller values than
+/-Infinity, you can use the constants
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
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
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.
- A substring of the original by picking individual letters or using
- A concatenation of two strings using the concatenation operator (
Beware of "stringly-typing" your code!
It can be tempting to use strings to represent complex data. Doing this comes with short-term benefits:
- It is easy to build complex strings with concatenation.
- Strings are easy to debug (what you see printed is always what is in the string).
- Strings are the common denominator of a lot of APIs (input fields, local storage values,
XMLHttpRequestresponses when using
responseText, etc.) and it can be tempting to only work with strings.
Use strings for textual data. When representing complex data, parse strings and use the appropriate abstraction.
In computer science, an object is a value in memory which is possibly referenced by an identifier.
There are two types of object properties which have certain attributes: The data property and the accessor property.
Associates a key with a value and has the following attributes:
|Read-only||Boolean||Reversed state of the ES5 [[Writable]] attribute.|
|DontEnum||Boolean||Reversed state of the ES5 [[Enumerable]] attribute.|
|DontDelete||Boolean||Reversed state of the ES5 [[Configurable]] attribute.|
Associates a key with one or two accessor functions (get and set) to retrieve or store a value and has the following attributes:
|[[Get]]||Function object or undefined||The function is called with an empty argument list and retrieves the property value whenever a get access to the value is performed. See also
|[[Set]]||Function object or undefined||The function is called with an argument that contains the assigned value and is executed whenever a specified property is attempted to be changed. See also
"Normal" objects, and functions
Functions are regular objects with the additional capability of being callable.
When representing dates, the best choice is to use the built-in
Indexed collections: Arrays and typed Arrays
Arrays are regular objects for which there is a particular relationship between integer-key-ed properties and the 'length' property. Additionally, arrays inherit from
Array.prototype which provides to them a handful of convenient methods to manipulate arrays. For example,
indexOf (searching a value in the array) or
push (adding an element to the array), etc. This makes Arrays a perfect candidate to represent lists or sets.
|Type||Size in bytes||Description||Web IDL type||Equivalent C type|
||1||8-bit two's complement signed integer||
||1||8-bit unsigned integer||
||1||8-bit unsigned integer (clamped)||
||2||16-bit two's complement signed integer||
||2||16-bit unsigned integer||
||4||32-bit two's complement signed integer||
||4||32-bit unsigned integer||
||4||32-bit IEEE floating point number||
||8||64-bit IEEE floating point number||
Keyed collections: Maps, Sets, WeakMaps, WeakSets
These data structures take object references as keys and are introduced in ECMAScript Edition 6.
WeakSet represent a set of objects, while
WeakMap associate a value to an object. 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.
One could implement Maps and Sets in pure ECMAScript 5. However, since objects cannot be compared (in the sense of "less than" for instance), 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.
Structured data: JSON
JSON for more details.
More objects in the standard library
Determining types using the
typeof operator can help you to find the type of your variable. Please read the reference page for more details and edge cases.
|ECMAScript 1st Edition (ECMA-262)||Standard||Initial definition.|
|ECMAScript 5.1 (ECMA-262)
The definition of 'Types' in that specification.
|ECMAScript 2015 (6th Edition, ECMA-262)
The definition of 'ECMAScript Data Types and Values' in that specification.