U
U
user20802021-06-09 17:48:04
JavaScript
user2080, 2021-06-09 17:48:04

What are the actual data types in javascript?

Hello!

As I understand it, there are two main data types in js (conditionally). These are primitive data types and objects.

Primitives are immutable, for example we have a string, we need to add a few more characters to it, so we concatenate.

let str = 'Hello'
let result = str + 'Vova'


1. What happens to the data type when str is concatenated with the string 'Vova' ?

As I understand the variable str with a data type (primitive) as a result of concatenation, a new value is created (still the same primitive), and which is no longer related to str in any way (I mean not referencing as an object by reference),

2. The question is in than, are primitives overwritten if some operations are performed on them? I'm interested in the creation algorithm itself, what happens inside.

3. Are primitive types objects?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Belyaev, 2021-06-10
@user2080

As I understand there are two main data types in js

According to the spec, there are 8 data types in JS:
  • object - all objects and functions (in reality, in all implementations, functions have their own function type)
  • string - strings
  • boolean - boolean values ​​(true and false)
  • number - double precision floating point numbers (in other languages ​​it is double or float64)
  • bigint - integers (int64) with support for long arithmetic
  • symbol - a special type, each value of which is unique
  • undefined - unit type with single value undefined, default value in many cases
  • null - unit type with a single null value (in reality, in all implementations, null has type object), conceived and used as an analogue of null pointer/null reference in other languages

What happens to the data type when str is concatenated with the string 'Vova' ?
nothing happens to the data type. The value is new, at least it should be according to the spec and this is how it looks for an ordinary js developer.
In fact, the most common js engine v8 does various magic under the hood with strings, knowing that strings never change, it can represent them as an array of slices (many pieces lying in different places), thereby avoiding expensive memory copying. In this example, result will consist of two slices that point to the memory representation of the code, with the first slice pointing to the same memory as str. But if result "survives" until the moment when the garbage collector does memory defragmentation, then after it it will already point to a whole independent line.
The question is, are primitives overwritten if some operations are performed on them? I'm interested in the creation algorithm itself, what happens inside
Primitives are never overwritten, at least while they are "alive" (until garbage collection recognizes them as garbage due to the lack of references to the memory where they lie). All manipulations with variables under the hood are almost always (I will write about when not - I will write below) simple manipulations over pointers (references).
Are primitive types objects?
From the point of view of the specs - no, they are not. They should have object wrappers when their fields are accessed (through a dot or square brackets).
In fact, if you do it like in the spec, everything will slow down terribly. Therefore, in v8, almost everything is an object (although not exactly the way we see objects from the js side), and compliance with the spec for primitive types is achieved through immutability.
The exceptions are null and undefined, which have neither fields nor a prototype. They only store type information.
Also, after compiler jit optimizations, numbers can get rid of object wrappers when the optimized code does not use them, thereby getting rid of pointer dereferencing, while bigint can turn into int64, and number into float64 or int32, depending on the use. Also, arrays consisting only of numbers of the same type can be reduced to real arrays of numbers, but this effect can be achieved before optimizations if typed arrays are used.

L
lssssssssssl, 2021-06-09
@lsssssssssl

1. A new result variable with the string type simply appears, which has nothing to do with str.
The type of a variable in js is determined at the time of assignment, not at the time of declaration. Each time you assign a new value to it, its type is determined.
2. "Variable" is just a property of a special internal object: Environment Record. "Get or change a variable" means "get or change a property of this object".
3. No. In this case, it can be confusing that in js it is possible to call a method on a primitive (For example, toUpperCase() on a string), this is possible because each primitive has a "wrapper object" that appears at the moment the variable is accessed (After is removed).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question