Esta tradução está incompleta. Ajude atraduzir este artigo.
var foo = 42; // foo é um Number agora var foo = "bar"; // foo é um String agora var foo = true; // foo é um Boolean agora
Tipos de Dados
A última versão ECMAScript define sete tipos de dados:
- Seis tipos de dados são: primitives:
- e Object
Todos os tipos exceto objetos definem valores imutáveis (valores que são incapazes de mudar). Por exemplo e diferente da linguagem C, Strings são imutáveis. Nós nos referimos a valores desses tipos como "valores primitivos".
representa uma entidade lógica e pode ter dois valores: verdadeiro(true) ou falso(false).
De acordo com os padrões ECMAScript, existe somente um tipo de number. O double-precision 64-bit binary format IEEE 754 value (número entre -(253 -1) e 253 -1). Não existe um tipo específico para inteiros. 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
- 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:
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 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.||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.