let foo = 42; // foo is now a number foo = 'bar'; // foo is now a string foo = true; // foo is now a boolean
The latest ECMAScript standard defines nine types:
- Six Data Types that are primitives, checked by typeof operator:
- Structural Types:
- Object :
typeof instance === "object". Special non-data but Structural type for any constructed object instance also used as data structures: new
Dateand almost everything made with new keyword;
- Function : a non-data structure, though it also answers for
typeof instance === "function". This is merely a special shorthand for Functions, though every Function constructor is derived from Object constructor.
- Object :
- Structural Root Primitive:
Keep in mind the only valuable purpose of
typeof operator usage is checking the Data Type. If we wish to check any Structural Type derived from Object it is pointless to use
typeof for that, as we will always receive
"object". The indeed proper way to check what sort of Object we are using is
instanceof keyword. But even in that case there might be misconceptions.
As we can see the meaning of every primitive type is obvious except of undefined and null which are almost the same. This happens as the concept of Time is strictly connected with the purpose of algorithms. We can purport something that does not yet exist or does not exist anymore: undefined. But when we wish to be able to represent something that exists being empty, we have to invent another keyword. And that is what null stands for: the beginning of structural meaning.
All types except objects define immutable values (that is, values which can't be changed). For example (and unlike in C), Strings are immutable. We refer to values of these types as "primitive values".
ECMAScript has two built-in numeric types: Number and BigInt (see below).
The Number type is a double-precision 64-bit binary format IEEE 754 value (numbers between -(253 − 1) and 253 − 1). In addition to representing floating-point numbers, the number type has three symbolic values:
NaN ("Not a Number").
Starting with ECMAScript 2015, 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 with two representations:
0 is represented as both
0 is an alias for
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
binary (bitwise) operators.
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.
BigInts, you can safely store and operate on large integers even beyond the safe integer limit for
BigInt is created by appending
n to the end of an integer or by calling the constructor.
You can obtain the safest 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
This example demonstrates, where incrementing the
Number.MAX_SAFE_INTEGER returns the expected result:
> const x = 2n ** 53n; 9007199254740992n > const y = x + 1n; 9007199254740993n
You can use the operators
BigInts—just like with
BigInt is not strictly equal to a
Number, but it is loosely so.
BigInt behaves like a
Number in cases where it is converted to
BigInts cannot be operated on interchangeably with
Numbers. Instead a
TypeError will be thrown.
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.
However, it is still possible to create another string based on an operation on the original string. For example:
- 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.
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".
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.
Object.defineProperty() to learn more.
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 of two accessor functions (
set) to retrieve or store a value, and has the following attributes:
|[[Get]]||Function object or
||The function is called with an empty argument list and retrieves the property value whenever a get access to the value is performed.
|[[Set]]||Function object or
||The function is called with an argument that contains the assigned value and is executed whenever a specified property is attempted to be changed.
Functions are regular objects with the additional capability of being callable.
When representing dates, the best choice is to use the built-in
Arrays are regular objects for which there is a particular relationship between integer-key-ed properties and the
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), and so on. This makes Arrays a perfect candidate to represent lists or sets.
|Type||Value Range||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 (7 significant digits e.g.,
||8||64-bit IEEE floating point number (16 significant digits e.g.,
||8||64-bit two's complement signed integer||
||8||64-bit unsigned integer||
The difference between
WeakMaps is that in the former, object keys can be enumerated over. This allows garbage collection optimizations in the latter case.
One could implement
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.
WeakMaps make it easy to privately bind data to an object.
Please have a look at the reference to find out about more objects.
typeof operator can help you to find the type of your variable.
Please read the reference page for more details and edge cases.
The definition of 'ECMAScript Data Types and Values' in that specification.