The binary object has three required properties


Programming languages all have built-in data structures, but these often differ from one language to another. This article attempts to list the built-in data structures available in JavaScript and what properties they have; these can be used to build other data structures.

When possible, comparisons with other languages are drawn. JavaScript is a loosely typed or a dynamic language. Variables in JavaScript are not directly associated with any particular value type, and any variable can be assigned and re-assigned values of all types:. All types except objects define immutable values values, which are incapable of being changed. For example and unlike to C, Strings are immutable.

We refer to values of these types as "primitive values". Boolean represents a logical entity and can have two values: The Null type has exactly one value: See null and Null for more details.

A variable that has not been assigned a value has the value undefined. See undefined and Undefined for more details. There is no specific type for integers. In addition to being able to represent floating-point numbers, the number type has three symbolic values: Beyond this range, integers in JavaScript are not safe anymore and will be a double-precision floating point approximation of the value.

The number type has only one integer that has two representations: In the praxis, this has almost no impact. However, you are able to notice this when you divide by zero:. 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.

However, this is usually considered a bad practice, since JavaScript offers 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 the 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. JavaScript's String type is used to represent textual data. It is a set of "elements" of 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. It can be tempting to use strings to represent complex data. Doing this comes with short-term benefits:. 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. 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 Symbol and the Symbol object wrapper in JavaScript. In computer science, an object is a value in memory which is possibly referenced by an identifier. In JavaScript, objects can be seen as a collection of properties.

With the object literal syntaxa 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.

Associates a key with one or two accessor functions get and set to retrieve or store a value and has the following attributes:. A JavaScript object is a mapping between keys and values. Keys are strings or Symbol s and values can be anything. This makes objects a natural fit for hashmaps. When representing dates, the best choice is to use the built-in Date utility in JavaScript. 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. For example, indexOf searching a value in the array or push adding an element to the arrayetc. This makes Arrays a perfect candidate to represent lists or sets. The following table helps you to find the equivalent C data types:. 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.

However, since objects cannot be compared in the sense of "less than" for instancelook-up performance would necessarily be linear.

Native implementations of them including WeakMaps can have look-up performance that is approximately logarithmic to constant time. 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. JSON builds universal data structures.

JavaScript has a standard library of built-in objects. Please have a look at the reference to find out about more objects. The typeof operator can help you to find the type of your variable. Please read the reference page for more details and edge cases.

Get the latest and greatest from MDN delivered straight to your inbox. Please check your inbox or your spam filter for an email from us. Document Tags and Contributors Tags: Contributors to this page: Learn the best of web development Get the latest and greatest from MDN delivered straight to your inbox.

I'm okay with Mozilla handling my info as explained in this Privacy Policy. Please check your inbox to confirm your subscription.

If truethe property will be enumerated in for See also Enumerability and ownership of properties. If falsethe property can't be deleted and attributes other than [[Value]] and [[Writable]] can't be changed.

The function is called with an empty argument list and retrieves the property value whenever a get access to the value is performed. The function is called with an argument that contains the assigned value and is executed whenever a specified property is attempted to be changed.