let foo = 42; // foo is now a number foo = 'bar'; // foo is now a string foo = true; // foo is now a boolean
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".
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 (
Note: You can check if a number is within the range of safe integers using
Number.isSafeInteger(). Outside the range from
Values outside the range ±(2^-1074 to 2^1024) are automatically converted:
- Positive values greater than
Number.MAX_VALUEare converted to
- Positive values smaller than
Number.MIN_VALUEare converted to
- Negative values smaller than -
Number.MAX_VALUEare converted to
- Negative values greater than -
Number.MIN_VALUEare converted to
The Number type has only one integer with multiple representations:
0 is represented as both
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:
console.log(42 / +0); // Infinity console.log(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.
A BigInt is created by appending
n to the end of an integer or by calling the constructor.
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
This example demonstrates, where incrementing the
Number.MAX_SAFE_INTEGER returns the expected result:
// 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 the operators
% 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:
BigInts cannot be operated on interchangeably with Numbers. Instead a
TypeError will be thrown.
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".
For more details see the
Symbol reference page.
In computer science, an object is a value in memory which is possibly referenced by an identifier.
Object.defineProperty(), or read them through
Object.getOwnPropertyDescriptor(). You can read more about the various nuances on the
Data properties associate a key with a value. It can be described by the following attributes:
A boolean value indicating if the property can be changed with an assignment.
A boolean value indicating if the property can be deleted, can be changed to an accessor property, and can have its attributes changed.
Associates a key with one of two accessor functions (
set) to retrieve or store a value.
An accessor property has the following attributes:
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
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
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.
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-keyed 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.
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
Float32Array, etc. Check the typed array page for more information.
The difference between
WeakMaps is that in the former, object keys can be enumerated over. This allows garbage collection optimizations in the latter case.
You could implement
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.
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.