Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 1031622 of JavaScript data types and data structures

  • Revision slug: Web/JavaScript/Data_structures
  • Revision title: JavaScript data types and data structures
  • Revision id: 1031622
  • Created:
  • Creator: witart
  • Is current revision? No
  • Comment

Revision Content

{{jsSidebar("More")}}

Semua bahasa pemrograman menyediakan struktur data built-in, namun berbeda antara bahasa yang satu dengan lainnya. Artikel ini berupaya membuat daftar struktur data built-in yang tersedia dalam JavaScript beserta properti yang dimilikinya; ini akan dapat digunakan untuk membentuk struktur data lainnya. Bila mungkin, perbandingan dengan bahasa lain dilakukan.

Tipe Dinamis

JavaScript merupakan suatu bahasa yang bertipe longgar atau bahasa dinamis. Itu berarti sebelumnya Anda tidak perlu mendeklarasikan tipe variabel. Tipe variabel akan ditentukan secara otomatis saat program sedang diproses. Itu juga berarti bahwa Anda dapat memiliki nama variabel yang sama dengan tipe yang berbeda:

var foo = 42;    // foo saat ini sebagai Numerik
var foo = "bar"; // foo saat ini sebagai String
var foo = true;  // foo saat ini sebagai Boolean

Tipe Data 

Standar ECMAScript terbaru mendefinisikan tujuh tipe data :

  • Enam tipe antara lain {{Glossary("Primitive", "primitives")}}:
    • {{Glossary("Boolean")}}
    • {{Glossary("Null")}}
    • {{Glossary("Undefined")}}
    • {{Glossary("Number")}}
    • {{Glossary("String")}}
    • {{Glossary("Symbol")}} (new in ECMAScript 6)
  • dan {{Glossary("Object")}}

Nilai-nilai Primitif 

Semua tipe kecuali objek mendefinisikan nilai-nilai tetap (nilai-nilai, yang tidak mampu berubah). Misalnya dan tidak seperti ke C, Strings yang tetap. Kami mengacu pada nilai-nilai dari tipe ini sebagai "nilai-nilai primitif".

Tipe Boolean 

Boolean merepresentasikan suatu entitas logika dan dapat memiliki dua nilai: benar, dan salah.

Tipe Null 

Tipe Null memiliki hanya satu nilai: null. Lihat {{jsxref("null")}} and {{Glossary("Null")}} untuk lebih jelasnya.

Tipe Undefined 

Suatu variabel yang belum ditetapkan dengan suatu nilai akan memilik nilai undefined. Lihat {{jsxref("undefined")}} and {{Glossary("Undefined")}} untuk lebih jelasnya.

Number type

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: +Infinity, -Infinity, and NaN (not-a-number).

To check for larger or smaller values than +/-Infinity, you can use the constants {{jsxref("Number.MAX_VALUE")}} or {{jsxref("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 {{jsxref("Number.isSafeInteger()")}} as well as {{jsxref("Number.MAX_SAFE_INTEGER")}} and {{jsxref("Number.MIN_SAFE_INTEGER")}}. Beyond this range, numbers in JavaScript are not safe anymore.

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

Although a number often represents only its value, JavaScript provides some binary operators. These can be used to represent several Boolean values within a single number using bit masking. This is usually considered a bad practice, however, JavaScript offers no other means to represent a set of Booleans (like an array of Booleans or an object with Boolean values assigned to named properties). Bit masking also tends to make code more difficult to read, understand, and maintain. It may be necessary to use such techniques in very constrained environments, like when trying to cope with the storage limitation of local storage or in extreme cases 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.

String type

JavaScript's {{jsxref("Global_Objects/String", "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.

Unlike in languages like C, JavaScript strings are immutable. This means that once a string is created, it is not possible to modify 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 {{jsxref("String.substr()")}}.
  • A concatenation of two strings using the concatenation operator (+) or {{jsxref("String.concat()")}}.

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, {{ domxref("XMLHttpRequest") }} responses when using responseText, etc.) and it can be tempting to only work with strings.

With conventions, it is possible to represent any data structure in a string. This does not make it a good idea. For instance, with a separator, one could emulate a list (while a JavaScript array would be more suitable). Unfortunately, when the separator is used in one of the "list" elements, then, the list is broken. An escape character can be chosen, etc. All of this requires conventions and creates an unnecessary maintenance burden.

Use strings for textual data. When representing complex data, parse strings and use the appropriate abstraction.

Symbol type

Symbols are new to JavaScript in ECMAScript Edition 6. 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. You can also compare them to named enumerations (enum) in C. For more details see {{Glossary("Symbol")}} and the {{jsxref("Symbol")}} object wrapper in JavaScript.

Objects

In computer science, an object is a value in memory which is possibly referenced by an {{Glossary("Identifier", "identifier")}}.

Properties

In JavaScript, objects can be seen as a collection of properties. With the object literal syntax, a limited set of properties are initialized; then properties can be added and removed. Property values can be values of any type, including other objects, which enables building complex data structures. Properties are identified using key values. A key value is either a String or a Symbol value.

There are two types of object properties which have certain attributes: The data property and the accessor property.

Data property

Associates a key with a value and has the following attributes:

Attributes of a data property
Attribute Type Description Default value
[[Value]] Any JavaScript type The value retrieved by a get access of the property. undefined
[[Writable]] Boolean If false, the property's [[Value]] can't be changed. false
[[Enumerable]] Boolean If true, the property will be enumerated in for...in loops. See also Enumerability and ownership of properties false
[[Configurable]] Boolean If false, the property can't be deleted and attributes other than [[Value]] and [[Writable]] can't be changed. false
Obsolete attributes (as of ECMAScript 3, renamed in ECMAScript 5)
Attribute Type Description
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.

Accessor property

Associates a key with one or two accessor functions (get and set) to retrieve or store a value and has the following attributes:

Attributes of an accessor property
Attribute Type Description Default value
[[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 get. undefined
[[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 set. undefined
[[Enumerable]] Boolean If true, the property will be enumerated in for...in loops. false
[[Configurable]] Boolean If false, the property can't be deleted and can't be changed to a data property. false

Note: Attribute is usually used by JavaScript engine, so you can't directly access it(see more about Object.defineProperty()).That's why the attribute is put in double square brackets instead of single.

"Normal" objects, and functions

A JavaScript object is a mapping between keys and values. Keys are strings (or {{jsxref("Symbol")}}s) and values can be anything. This makes objects a natural fit for hashmaps.

Functions are regular objects with the additional capability of being callable.

Dates

When representing dates, the best choice is to use the built-in Date utility in JavaScript.

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.

Typed Arrays are new to JavaScript with ECMAScript Edition 6 and present an array-like view of an underlying binary data buffer. The following table helps you to find the equivalent C data types:

{{page("/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray", "TypedArray_objects", "", 0, 3)}}

Keyed collections: Maps, Sets, WeakMaps, WeakSets

These data structures take object references as keys and are introduced in ECMAScript Edition 6. {{jsxref("Set")}} and {{jsxref("WeakSet")}} represent a set of objects, while {{jsxref("Map")}} and {{jsxref("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 (JavaScript Object Notation) is a lightweight data-interchange format, derived from JavaScript but used by many programming languages. JSON builds universal data structures. See {{Glossary("JSON")}} and {{jsxref("JSON")}} for more details.

More objects in the standard library

JavaScript has a standard library of built-in objects. Please have a look at the reference to find out about more objects.

Determining types using the typeof operator

The typeof operator can help you to find the type of your variable. Please read the reference page for more details and edge cases.

Specifications

Specification Status Comment
{{SpecName('ES1')}} {{Spec2('ES1')}} Initial definition.
{{SpecName('ES5.1', '#sec-8', 'Types')}} {{Spec2('ES5.1')}}  
{{SpecName('ES6', '#sec-ecmascript-data-types-and-values', 'ECMAScript Data Types and Values')}} {{Spec2('ES6')}}  
{{SpecName('ESDraft', '#sec-ecmascript-data-types-and-values', 'ECMAScript Data Types and Values')}} {{Spec2('ESDraft')}}  

See also

Revision Source

<div>{{jsSidebar("More")}}</div>

<p>Semua bahasa pemrograman menyediakan struktur data built-in, namun berbeda antara bahasa yang satu dengan lainnya. Artikel ini berupaya membuat daftar struktur data built-in yang tersedia dalam JavaScript beserta properti yang dimilikinya; ini akan dapat digunakan untuk membentuk struktur data lainnya.&nbsp;Bila mungkin, perbandingan dengan bahasa lain dilakukan.</p>

<h2 id="Dynamic_typing">Tipe Dinamis</h2>

<p>JavaScript merupakan suatu bahasa yang bertipe longgar atau bahasa dinamis. Itu berarti sebelumnya&nbsp;Anda tidak perlu mendeklarasikan tipe variabel. Tipe variabel&nbsp;akan ditentukan secara otomatis saat program sedang diproses. Itu juga berarti bahwa Anda dapat memiliki nama variabel yang sama dengan tipe yang berbeda:</p>

<pre class="brush: js">
var foo = 42;    // foo saat ini sebagai Numerik
var foo = "bar"; // foo saat ini sebagai String
var foo = true;  // foo saat ini sebagai Boolean
</pre>

<h2 id="Data_types">Tipe Data&nbsp;</h2>

<p>Standar ECMAScript terbaru mendefinisikan tujuh tipe data :</p>

<ul>
 <li>Enam tipe antara lain&nbsp;{{Glossary("Primitive", "primitives")}}:
  <ul>
   <li>{{Glossary("Boolean")}}</li>
   <li>{{Glossary("Null")}}</li>
   <li>{{Glossary("Undefined")}}</li>
   <li>{{Glossary("Number")}}</li>
   <li>{{Glossary("String")}}</li>
   <li>{{Glossary("Symbol")}} (new in ECMAScript 6)</li>
  </ul>
 </li>
 <li>dan {{Glossary("Object")}}</li>
</ul>

<h2 id="Primitive_values">Nilai-nilai Primitif&nbsp;</h2>

<p>Semua tipe kecuali objek mendefinisikan nilai-nilai tetap (nilai-nilai, yang tidak mampu berubah). Misalnya dan tidak seperti ke C, Strings yang tetap. Kami mengacu pada nilai-nilai dari tipe ini sebagai "nilai-nilai primitif".</p>

<h3 id="Boolean_type">Tipe Boolean&nbsp;</h3>

<p>Boolean merepresentasikan suatu entitas logika&nbsp;dan dapat memiliki dua nilai: benar, dan salah.</p>

<h3 id="Null_type">Tipe Null&nbsp;</h3>

<p>Tipe Null memiliki hanya satu nilai: null. Lihat {{jsxref("null")}} and {{Glossary("Null")}} untuk lebih jelasnya.</p>

<h3 id="Undefined_type">Tipe Undefined&nbsp;</h3>

<p>Suatu variabel yang belum ditetapkan dengan&nbsp;suatu&nbsp;nilai akan memilik nilai <code>undefined</code>. Lihat {{jsxref("undefined")}} and {{Glossary("Undefined")}} untuk lebih jelasnya.</p>

<h3 id="Number_type">Number type</h3>

<p>According to the ECMAScript standard, there is only one number type: the <a href="https://en.wikipedia.org/wiki/Double_precision_floating-point_format">double-precision 64-bit binary format IEEE 754 value</a> (number between -(2<sup>53</sup> -1) and 2<sup>53</sup> -1). <strong>There is no specific type for integers</strong>. In addition to being able to represent floating-point numbers, the number type has three symbolic values: <code>+Infinity</code>, <code>-Infinity</code>, and <a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/NaN"><code>NaN</code></a> (not-a-number).</p>

<p>To check for larger or smaller values than <code>+/-Infinity</code>, you can use the constants {{jsxref("Number.MAX_VALUE")}} or {{jsxref("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 {{jsxref("Number.isSafeInteger()")}} as well as {{jsxref("Number.MAX_SAFE_INTEGER")}} and {{jsxref("Number.MIN_SAFE_INTEGER")}}. Beyond this range, numbers in JavaScript are not safe anymore.</p>

<p>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 <code>+0 === -0</code> is <code>true</code>. However, you are able to notice this when you divide by zero:</p>

<pre class="brush: js">
&gt; 42 / +0
Infinity
&gt; 42 / -0
-Infinity
</pre>

<p>Although a number often represents only its value, JavaScript provides <a href="/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators" title="en/JavaScript/Reference/Operators/Bitwise_Operators">some binary operators</a>. These can be used to represent several Boolean values within a single number using <a class="external" href="https://en.wikipedia.org/wiki/Mask_%28computing%29">bit masking</a>. This is usually considered a bad practice, however, JavaScript offers no other means to represent a set of Booleans (like an array of Booleans or an object with Boolean values assigned to named properties). Bit masking also tends to make code more difficult to read, understand, and maintain. It may be necessary to use such techniques in very constrained environments, like when trying to cope with the storage limitation of local storage or in extreme cases 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.</p>

<h3 id="String_type">String type</h3>

<p>JavaScript's {{jsxref("Global_Objects/String", "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.</p>

<p>Unlike in languages like C, JavaScript strings are immutable. This means that once a string is created, it is not possible to modify it. However, it is still possible to create another string based on an operation on the original string. For example:</p>

<ul>
 <li>A substring of the original by picking individual letters or using {{jsxref("String.substr()")}}.</li>
 <li>A concatenation of two strings using the concatenation operator (<code>+</code>) or {{jsxref("String.concat()")}}.</li>
</ul>

<h4 id="Beware_of_stringly-typing_your_code!">Beware of "stringly-typing" your code!</h4>

<p>It can be tempting to use strings to represent complex data. Doing this comes with short-term benefits:</p>

<ul>
 <li>It is easy to build complex strings with concatenation.</li>
 <li>Strings are easy to debug (what you see printed is always what is in the string).</li>
 <li>Strings are the common denominator of a lot of APIs (<a href="/en-US/docs/Web/API/HTMLInputElement" title="HTMLInputElement">input fields</a>, <a href="/en-US/docs/Storage" title="Storage">local storage</a> values, {{ domxref("XMLHttpRequest") }} responses when using <code>responseText</code>, etc.) and it can be tempting to only work with strings.</li>
</ul>

<p>With conventions, it is possible to represent any data structure in a string. This does not make it a good idea. For instance, with a separator, one could emulate a list (while a JavaScript array would be more suitable). Unfortunately, when the separator is used in one of the "list" elements, then, the list is broken. An escape character can be chosen, etc. All of this requires conventions and creates an unnecessary maintenance burden.</p>

<p>Use strings for textual data. When representing complex data, parse strings and use the appropriate abstraction.</p>

<h3 id="Symbol_type">Symbol type</h3>

<p>Symbols are new to JavaScript in ECMAScript Edition 6. A Symbol is a <strong>unique</strong> and <strong>immutable</strong> primitive value and may be used as the key of an Object property (see below). In some programming languages, Symbols are called atoms. You can also compare them to named enumerations (enum) in C. For more details see {{Glossary("Symbol")}} and the {{jsxref("Symbol")}} object wrapper in JavaScript.</p>

<h2 id="Objects">Objects</h2>

<p>In computer science, an object is a value in memory which is possibly referenced by an {{Glossary("Identifier", "identifier")}}.</p>

<h3 id="Properties">Properties</h3>

<p>In JavaScript, objects can be seen as a collection of properties. With the <a href="/en-US/docs/Web/JavaScript/Guide/Values,_variables,_and_literals#Object_literals">object literal syntax</a>, a limited set of properties are initialized; then properties can be added and removed. Property values can be values of any type, including other objects, which enables building complex data structures. Properties are identified using key values. A key value is either a String or a Symbol value.</p>

<p>There are two types of object properties which have certain attributes: The data property and the accessor property.</p>

<h4 id="Data_property">Data property</h4>

<p>Associates a key with a value and has the following attributes:</p>

<table class="standard-table">
 <caption>Attributes of a data property</caption>
 <tbody>
  <tr>
   <th>Attribute</th>
   <th>Type</th>
   <th>Description</th>
   <th>Default value</th>
  </tr>
  <tr>
   <td>[[Value]]</td>
   <td>Any JavaScript type</td>
   <td>The value retrieved by a get access of the property.</td>
   <td>undefined</td>
  </tr>
  <tr>
   <td>[[Writable]]</td>
   <td>Boolean</td>
   <td>If <code>false</code>, the property's [[Value]] can't be changed.</td>
   <td>false</td>
  </tr>
  <tr>
   <td>[[Enumerable]]</td>
   <td>Boolean</td>
   <td>If <code>true</code>, the property will be enumerated in <a href="/en-US/docs/Web/JavaScript/Reference/Statements/for...in">for...in</a> loops. See also <a href="/en-US/docs/Web/JavaScript/Enumerability_and_ownership_of_properties">Enumerability and ownership of properties</a></td>
   <td>false</td>
  </tr>
  <tr>
   <td>[[Configurable]]</td>
   <td>Boolean</td>
   <td>If <code>false</code>, the property can't be deleted and attributes other than [[Value]] and [[Writable]] can't be changed.</td>
   <td>false</td>
  </tr>
 </tbody>
</table>

<table class="standard-table">
 <caption>Obsolete attributes (as of ECMAScript 3, renamed in ECMAScript 5)</caption>
 <tbody>
  <tr>
   <th>Attribute</th>
   <th>Type</th>
   <th>Description</th>
  </tr>
  <tr>
   <td>Read-only</td>
   <td>Boolean</td>
   <td>Reversed state of the ES5 [[Writable]] attribute.</td>
  </tr>
  <tr>
   <td>DontEnum</td>
   <td>Boolean</td>
   <td>Reversed state of the ES5 [[Enumerable]] attribute.</td>
  </tr>
  <tr>
   <td>DontDelete</td>
   <td>Boolean</td>
   <td>Reversed state of the ES5 [[Configurable]] attribute.</td>
  </tr>
 </tbody>
</table>

<h4 id="Accessor_property">Accessor property</h4>

<p>Associates a key with one or two accessor functions (get and set) to retrieve or store a value and has the following attributes:</p>

<table class="standard-table">
 <caption>Attributes of an accessor property</caption>
 <tbody>
  <tr>
   <th>Attribute</th>
   <th>Type</th>
   <th>Description</th>
   <th>Default value</th>
  </tr>
  <tr>
   <td>[[Get]]</td>
   <td>Function object or undefined</td>
   <td>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 <a href="/en-US/docs/Web/JavaScript/Reference/Operators/get"><code>get</code></a>.</td>
   <td>undefined</td>
  </tr>
  <tr>
   <td>[[Set]]</td>
   <td>Function object or undefined</td>
   <td>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 <a href="/en-US/docs/Web/JavaScript/Reference/Operators/set"><code>set</code></a>.</td>
   <td>undefined</td>
  </tr>
  <tr>
   <td>[[Enumerable]]</td>
   <td>Boolean</td>
   <td>If <code>true</code>, the property will be enumerated in <a href="/en-US/docs/Web/JavaScript/Reference/Statements/for...in">for...in</a> loops.</td>
   <td>false</td>
  </tr>
  <tr>
   <td>[[Configurable]]</td>
   <td>Boolean</td>
   <td>If <code>false</code>, the property can't be deleted and can't be changed to a data property.</td>
   <td>false</td>
  </tr>
 </tbody>
</table>

<div class="note">
<p><strong>Note: </strong>Attribute is usually&nbsp;used by JavaScript engine, so you can't&nbsp;directly access it(see more about&nbsp;<a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty">Object.defineProperty()</a>).That's why the attribute is put in double square brackets&nbsp;instead of single.</p>
</div>

<h3 id="Normal_objects_and_functions">"Normal" objects, and functions</h3>

<p>A JavaScript object is a mapping between keys and values. Keys are strings (or {{jsxref("Symbol")}}s) and values can be anything. This makes objects a natural fit for <a class="external" href="https://en.wikipedia.org/wiki/Hash_table">hashmaps</a>.</p>

<p>Functions are regular objects with the additional capability of being callable.</p>

<h3 id="Dates">Dates</h3>

<p>When representing dates, the best choice is to use the built-in <a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date"><code>Date</code> utility</a> in JavaScript.</p>

<h3 id="Indexed_collections_Arrays_and_typed_Arrays">Indexed collections: Arrays and typed Arrays</h3>

<p><a href="/en-US/docs/JavaScript/Reference/Global_Objects/Array" title="Array">Arrays</a> are regular objects for which there is a particular relationship between integer-key-ed properties and the 'length' property. Additionally, arrays inherit from <code>Array.prototype</code> which provides to them a handful of convenient methods to manipulate arrays. For example, <code><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf" title="en/JavaScript/Reference/Global_Objects/Array/indexOf">indexOf</a></code> (searching a value in the array) or <code><a href="/en-US/docs/JavaScript/Reference/Global_Objects/Array/push" title="en/JavaScript/Reference/Global_Objects/Array/push">push</a></code> (adding an element to the array), etc. This makes Arrays a perfect candidate to represent lists or sets.</p>

<p><a href="/en-US/docs/Web/JavaScript/Typed_arrays">Typed Arrays</a> are new to JavaScript with ECMAScript Edition 6 and present an array-like view of an underlying binary data buffer. The following table helps you to find the equivalent C data types:</p>

<p>{{page("/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray", "TypedArray_objects", "", 0, 3)}}</p>

<h3 id="Keyed_collections_Maps_Sets_WeakMaps_WeakSets">Keyed collections: Maps, Sets, WeakMaps, WeakSets</h3>

<p>These data structures take object references as keys and are introduced in ECMAScript Edition 6. {{jsxref("Set")}} and {{jsxref("WeakSet")}} represent a set of objects, while {{jsxref("Map")}} and {{jsxref("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.</p>

<p>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.</p>

<p>Usually, to bind data to a DOM node, one could set properties directly on the object or use <code>data-*</code> 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.</p>

<h3 id="Structured_data_JSON">Structured data: JSON</h3>

<p>JSON (JavaScript Object Notation) is a lightweight data-interchange format, derived from JavaScript but used by many programming languages. JSON builds universal data structures. See {{Glossary("JSON")}} and {{jsxref("JSON")}} for more details.</p>

<h3 id="More_objects_in_the_standard_library">More objects in the standard library</h3>

<p>JavaScript has a standard library of built-in objects. Please have a look at the <a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects">reference</a> to find out about more objects.</p>

<h2 id="Determining_types_using_the_typeof_operator">Determining types using the <code>typeof</code> operator</h2>

<p>The <code>typeof</code> operator can help you to find the type of your variable. Please read the <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof">reference page</a> for more details and edge cases.</p>

<h2 id="Specifications">Specifications</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('ES1')}}</td>
   <td>{{Spec2('ES1')}}</td>
   <td>Initial definition.</td>
  </tr>
  <tr>
   <td>{{SpecName('ES5.1', '#sec-8', 'Types')}}</td>
   <td>{{Spec2('ES5.1')}}</td>
   <td>&nbsp;</td>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#sec-ecmascript-data-types-and-values', 'ECMAScript Data Types and Values')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td>&nbsp;</td>
  </tr>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-ecmascript-data-types-and-values', 'ECMAScript Data Types and Values')}}</td>
   <td>{{Spec2('ESDraft')}}</td>
   <td>&nbsp;</td>
  </tr>
 </tbody>
</table>

<h2 id="See_also">See also</h2>

<ul>
 <li><a class="link-https" href="https://github.com/nzakas/computer-science-in-javascript/">Nicholas Zakas collection of common data structure and common algorithms in JavaScript.</a></li>
 <li><a href="https://github.com/monmohan/DataStructures_In_Javascript" title="https://github.com/monmohan/DataStructures_In_Javascript">Search Tre(i)es implemented in JavaScript</a></li>
</ul>
Revert to this revision