Data types in JavaScript

Data types in JavaScript

Division of data types

data types in javascript

There are 7 data types within JavaScript. Data can be divided into two groups:
a) Data that store primitive value type:

  • 1) string
  • 2) number
  • 3) boolean
  • 4) undefined
  • 5) null
  • 6) symbol (ES2015)

b) Data that stores reference value type:

  • 7) object
    • array
    • Map (ES2015)
    • WeekMap (ES2015)
    • Set (ES2015)
    • WeekSet (ES2015)

Characteristics of primitive value type

Primitive value type is stored in a fast part of memory called “stack” and has an immutable (“immutable“) value. This means that after creation and storage in memory, that value at that location cannot be changed. Their immutability is the reason that for comparing variables containing a primitive value type, it is enough to compare only the values. The characteristics of primitive values are best described through an example:

Example

a) case: Assigning a primitive value type to a variable. This procedure implies:

  • Generating the value “Dargoljub”, which occupies a certain space in memory
  • Defining a pointer to the variable “person1” to point to that place

b) case: Assigning an already existing variable to a new variable. This procedure implies:

  1. Taking a new place in memory to copy the assigned value of another variable
  2. Defining a variable pointer to point to that new location in memory

It should be emphasized that the two variables from the previous examples (person1, person2) point to two different places in memory with equal values. If we assign a new value to the variable “person1”, it will not affect the variable “person2”, because it points to the place in memory where the value “Dargoljub” is stored:

Characteristics of the reference value type

A reference value type is stored in a slower part of memory called the “heap”, and unlike a primitive value type a reference value type can change over time. Assigning a reference value to a variable performs the same action as with a primitive: the value occupies a place in memory, and the pointervariable is defined to point to that location in memory.

For two objects to be equal, it is not enough that they have the same content, they must also point to the same place in memory.

Example

With a value reference type, assigning an existing variable to a new variable only creates a pointer to the new variable at the same place that the existing variable already points to (it does not copy its value and does not take up a new place in memory):

Therefore, after some change in the referent value, there is a change in both variables because they both point to the same place in memory:

NOTE:

It should be emphasized that whenever we create an object via the constructor “new” or “object literal”, a new place in memory is occupied and that object is different from every other object:

Example

In the following example, person2.name returns “Dragoljub” even though one would expect it to be “Pera” precisely for the previously mentioned reason, because assigning an object literal takes a new place in memory, while persona2 still points to the previous position where the name value is still “Dragoljub”.

For the same reason, the following example returns FALSE

Is everything in JavaScript an object?

Programmers often state that everything in JavaScript is an object, but this is not a correct statement because an object is only one of the data types in JavaScript, and there are 6 other primitive types besides the object. Confusion is also caused by the fact that there are objects built into core javascript whose names are the same as the names of simple primitives (although the names start with a capital letter!).

  • String
  • Number
  • Boolean

In addition to the “unfortunate” names for built-in objects, additional confusion is created by the automatic conversion of a primitive type into its “counterpart” object. Conversion is not the best term, it would be more accurate to say that the compiler automatically temporarily “wraps” the primitive into a counterpart object. Automatic conversion most often happens when the programmer adds one of the methods of the “counterpart” object to the primitive.

Example

In this example, the method of the object “String” is called on the primitive of type “string”. The compiler automatically temporarily “converts” a primitive of type “string” into an object of type String, after which all methods of the built-in object are available to it, including the method length:

An additional reason why programmers confuse primitives with objects is:

The preceding expression is one of the most famous bugs in JavaScript, it is believed that this bug will probably never be fixed, because it is already too late, I will quote:

“Too much code on the Web relies on the bug and thus fixing it would cause a lot more bugs!”

And what are Functions?

The expression typeof returns “function”, so it is safe to say that the function is not of type “object” but of type “function”.

Since in JavaScript, the so-called “first class citizen”, she has the traits of all primitives as well as all traitsobject!

EXPLANATION:
The term “first class citizen” means that such an entity supports all operations available to other types, and can behave like any other type of entity. Overall, since functions are a “first class citizen” in JavaScript, there are no restrictions on how they can be created or used.

A function has all the properties of other primitives, so we can pass it as an argument, we can return it (return) as a value from another function or we can assign it to a variable. In addition to the listed properties, functions have all the properties of objects. Functions are “more than an object” because in addition to all the properties of an object, they also have properties of primitives, as well as added semantics for calling (eng. invoke).