How many types of JS are there

JavaScript data types and data structures

All programming languages ​​have built-in data structures. However, these differ depending on the programming language. This article attempts to explain the data structures available in JavaScript and their properties. In addition, comparisons with other programming languages ​​are made at appropriate points.

Dynamic typing

JavaScript is a weakly typed or dynamic Programming language. Data types are not explicitly declared for a variable and each variable can be written (overwritten) with values ​​of any type:

Data types

The current ECMAScript standard defines seven data types:

Primitive values

All data types, with the exception of Object, define unchangeable values ​​(values ​​that cannot be changed). For example, unlike C, strings are immutable. The values ​​of these data types are called "primitive values".

Boolean data type

Boolean represents a logical unit and can have two values: and.

Null data type

The null type has exactly one value:. See and zero for more details.

Undefined data type

A variable that has not yet been assigned a value has the value. See and Undefined for more details.

Number data type

According to the ECMAScript standard there is only one data type for numbers: double-precision 64-bit binary format IEEE 754 value (numbers between - (263) and +263-1). There is no specific data type for integers. In addition to floating point numbers, the data type for numbers can also have three symbolic values:, and (not-a-number = no number).

The constants or can be used to check for values ​​greater or less than. As of ECMAScript 2015, it can also be checked whether a number is within the range of floating point numbers with double precision. That is possible with as well as and. Numbers outside of this range are no longer safe in JavaScript.

The data type for numbers has an integer with two representations: 0 can be represented as -0 and +0. ("0" is an alias for +0). In practice this has almost no effect. Is for example . However, a division gives two different values:

Although a number often represents its value, JavaScript offers some binary operators (en-US). They can be used, for example, to represent several Boolean values ​​within a single number using bit masking. This is generally considered bad practice, as JavaScript offers other ways to represent a set of Booleans (such as an array of Booleans or an object with Boolean values ​​as named properties). Bit masking also makes code harder to read, understand, and maintain. Bit masking may be necessary in very special areas of application. For example, when only a small amount of memory is available or in extreme cases when every bit transferred in the network counts. However, this technique should be seen as a last resort to optimize the size.

String data type

The String data type is used in JavaScript to represent textual data. It is a set of "elements" of 16-bit unsigned integer values. Each element in the string takes a position in the string. The first element is at the position with index 0, the next at index 1 and so on. The length of a string is the number of elements in it.

In contrast to other programming languages ​​such as C, a string in JavaScript cannot be changed. This means that a string cannot be changed after it has been created. But it is possible to create another string with the help of operations from the original string:

  • A substring of the original string by selecting certain characters or using the method.
  • A concatenation of two strings with the operator () or.

Avoid "stringly-typing" in your code!

String can be tempting to represent complex data. It has short-term benefits:

  • Complex strings can be created simply by concatenation.
  • Strings are easier to debug (the output corresponds to the content of the string).
  • Strings are used in many APIs (input fields, local storage values, responses in their capacity as, etc.). This can make it tempting to work with strings only.

Without conventions, any data structure can be represented as a string. But that's not a good idea. For example, a list could be emulated by separating the list elements in the string with a separator. In this case, a JavaScript array is more suitable, because the list can be destroyed as soon as a list element contains the separator. An escape character or something similar would have to be used. However, this requires additional conventions and the maintenance of the code increases in complexity.

Strings should only be used for textual data. If you want to represent complex data, parse the string and use an appropriate abstraction.

Symbol data type

Symbols in JavaScript were introduced with ECMAScript 2015. A symbol is a clearer and immutable primitive value. It can be used as a key to a property of an object (see below). In some programming languages ​​symbols are also called atoms. Details can be found under Symbol and the Object Wrapper in JavaScript.

Objects

In computer science, objects are values ​​in memory that may be referenced using an identifier.

properties

In JavaScript, objects can be viewed as a collection of properties. The most necessary properties are initialized with the object literal syntax. Properties can then be added or removed again. The value of a property can be of any data type. There can be other objects underneath. This means that complex data structures can be implemented. Properties are identified with a key. A key is either a string or a symbol.

There are two types of properties that have certain attributes: the data properties and access properties.

Data properties

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

attributeData typedescriptiondefault value
[[Value]]any JavaScript data typeThe value that is returned when the property is accessedundefined
[[Writable]]Booleanif [[Value]] of the property cannot be changed.false
[[Enumerable]]Booleanif the property in for ... is listed in loops. See also enumerability and ownership of properties.false
[[Configurable]]Booleanif the property cannot be deleted and the attributes [[Value]] and [[Writable]] cannot be changed.false

Access properties

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

attributeData typedescriptiondefault value
[[Get]]Function object or undefinedThe function is called without an argument and returns the value of the property as soon as a get access is performed on the value. See also get (en-US).undefined
[[Set]]Function object or undefinedThe function is called with an argument that contains the value for the assignment. It is always called as soon as a certain property is to be changed. See also set (en-US).undefined
[[Enumerable]]Booleanif the property in for ... is listed in loops.false
[[Configurable]]Booleanif the property cannot be deleted and changed to a data property.false

Note: Attribute is normally used by the JavaScript environment and cannot be accessed directly (more about this: Object.defineProperty ()). Therefore the attributes are in double square brackets instead of single ones.

"Normal" objects and functions

An object in JavaScript is a mapping between keys and values. Keys are strings or symbols and values ​​can be of any data type. This makes objects a kind of hash table.

Functions are regular objects with the ability to be called directly.

date

A date can be represented with the utility built into JavaScript.

Indexed Collections: Arrays and Typed Arrays

Arrays are regular objects in which there is a relationship between properties, which are indexed via integers, and the property 'length'. In addition, arrays inherit from, a helper method that offers manipulation of arrays, such as (to find a value in an array) or (to add elements to an array). This allows lists and quantities to be represented via an array.

Typed arrays were introduced in JavaScript with ECMAScript Edition 2015. They offer an array-like view of an underlying binary data buffer. The following table shows equivalent C data types:

TypedArray objects

TypeValue rangeSize in bytesDescriptionWeb IDL typeEquivalent C type
to 18-bit two's complement signed integer
to 18-bit unsigned integer
to 18-bit unsigned integer (clamped)
to 216-bit two's complement signed integer
to 216-bit unsigned integer
to 432-bit two's complement signed integer
to 432-bit unsigned integer
to 432-bit IEEE floating point number (7 significant digits e.g.,)
to 864-bit IEEE floating point number (16 significant digits e.g.,)
to 864-bit two's complement signed integer
to 864-bit unsigned integer

Keyed Collections: Maps, Sets, WeakMaps, WeakSets

These data structures use object references as keys. They were introduced with ECMAScript Edition 6. and represent a set of objects, while and associate a value with an object. The difference between Maps and WeakMaps is that maps can be iterated over object keys. This allows a later optimization of the garbage collection.

Maps and sets could be implemented in pure ECMAScript 5. However, since objects cannot be compared (in the sense of, for example, "less than or equal to"), the performance for a look-up would be linear. Native implementations (including weak maps) can have an approximately logarithmic look-up performance.

To bind data to a DOM node, the properties are usually set directly on the object or attributes are used. However, this has the disadvantage that the data is available in the same context for each script. With Maps and WeakMaps, data can easily be linked privately to an object.

Structured data: JSON

JSON (JavaScript Object Notation) is a lightweight format for data exchange that is derived from JavaScript but is also used by many other programming languages. Universal data structures can be built with JSON. See JSON and for more details.

More objects in the standard library

JavaScript has a standard library with numerous built-in objects. Details can be found under reference.

Determine data types with the operator

The operator can find out the data type of a variable. More details and borderline cases can be found on the reference page.

Specifications

See also