Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

207
Vistas
If I change a variable's value will the original value still exist inside the memory?
let a = 7;
a = 3;
console.log(a); // output 3

In this case, the value 7 is still inside a memory? I was reading that all primitive data types are immutable.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

The value of a is kept in memory, until garbage collection eventually recycles it. What the docs mean by immutable is that you can't directly alter the primative (in this case the integer 7). You can only replace the value.

There are examples on the docs but this is another one

let a = 1;
a.toString() // a is still 1, it cannot be mutated

However, we can assign this to another variable

let a = 1;
let b = a.toString() // b is string "1" and a remains as the integer 1

Or we can replace the value

let a = 1;
a = 10; // a is 10
about 4 years ago · Juan Pablo Isaza Denunciar

0

Short answer: no.

Immutable is about the behaviour of the value, not about what happens when it's no longer being references: you cannot change an immutable value, you have to instead reassign your variable if you want it to point to a new value.

let b = 3;
let a = b; // the variable "a" points to the primitive value 3
b = 7;     // the variable "a" still points to the primitive value 3
a = 7;     // and now it points to a _different_ value. 7, in this case.

This is in contrast to things like arrays or objects, which you can change as much as you like without needing to reassign:

const obj = {};
const a = obj;
obj.cat = `meow`;
console.log(a); // this will show that "a" is { cat: "meow" }

We just changed the content of the value that "a" points to, without any reassignments.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda