Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

204
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!