var foo = 42; // foo 目前是數字 var foo = "bar"; // foo 目前是字串 var foo = true; // foo 目前是布林值
除了物件以外的所有值，都是原始定義的值（值意味著不能被改變）。例如與 C 不同的地方，就是字串是不變的。我們引用這些類型的值為 primitive values（原始值）。
Number.MAX_VALUE (en-US) 或
Number.MIN_VALUE (en-US) 常數。從 ECMAScript 6 開始，也可以透過
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
字串型別用來代表文字資料。它是一組 16 位的未宣告「元素」值。每個字串的元素，在字串內皆佔有一位。第一個元素位於索引的第 0 位，下一個元素位於第 1 位，並依此類推。字串的長度，是指該字串有多少元素。
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 (en-US), 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.
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
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 a handful of convenient methods to manipulate arrays. For example,
indexOf() searches a value in the array and
push() appends an element to the array. This makes Arrays a perfect candidate to represent ordered lists.
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. Typed arrays are often used in conjunction with
These data structures take object references as keys and are introduced in ECMAScript Edition 6.
WeakSet (en-US) represent a set of objects, while
WeakMap (en-US) 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.
typeof 運算子可以幫助你找到你的變數型別，請閱讀〈reference page 〉來取得更多細節及邊緣案例。