Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

217
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda