Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why number are immutable in Javascript?

I have read the question and answer here:

javascript numbers- immutable

But it's not enough clear for me why the number (primitive type) are immutable? Just because they create a new reference but not overwrite the value?

If on each assignemt is created a new reference

var x = 5;
x = 1;

Would we have 100 times a new reference in the following loop?

while (x < 101)
{
    x++;
}

Is that efficient? I think I am not seeing correctly.

like image 396
Jamo Avatar asked Sep 24 '17 16:09

Jamo


People also ask

Why are strings and numbers immutable in JavaScript?

They are immutable primitives. This means that the characters within them may not be changed and that any operations on strings actually create new strings. It helps to think of strings in the way that we think of numbers, if we are to better understand how they work. Numeric values are also immutable primitives.

Is number mutable in JavaScript?

In JavaScript, objects and arrays are mutable by default, but primitive values are not — once a primitive value is created, it cannot be changed, although the variable that holds it may be reassigned.

Why is integer data considered immutable?

int is immutable in nature, so we can't change or update the int data-type. As we read earlier, that immutable objects change their memory address when they get updated. We assigned some integer value to a variable. Then, we print the id of that variable.

What are immutable values in JavaScript?

In JavaScript String and Numbers are immutable data types. JavaScript is not a good language to work with data in an immutable fashion. In JavaScript Arrays and Objects are not immutable. Their values can be changed over time.


2 Answers

I'm honestly not quite sure what kind of answer you expect since I don't quite understand what you are confused about. But here we go:

Would we have 100 times a new reference in the following loop?

Variables are just containers for values. At a low level a variable is basically just a label for a memory address or a register. E.g. variable x might point to register R1.

x++ would simply increment the number that is stored in that register by 1. Lets assume our register looked like this:

R1: 5

After incrementing it, which can be a single operation, such as ADD R1 1, we would get

R1: 6

I.e. we simple overwrote the previous value with a new one. And we do that multiple times.


Is that efficient? I think I am not seeing correctly.

Incrementing a number by one is as simple of an operation as it can get.

Sure, you could implement mutable numbers on a higher level, but it certainly wouldn't make things more efficient or simpler.

Mutability doesn't make much sense for "single value" values, because mutating such a value basically means replacing it with a different value "in place".

Mutability makes more sense for values that are composed of other values such as lists and dictionaries, where one part changes and the other stays the same.

Additionally, mutability only seems relevant when a language has reference type data types. With that I mean that multiple variables can hold a reference to the very same value of a data type. Objects are reference-type in JavaScript, which allows you to do this:

var a = {foo: 42};
var b = a;
b.foo = 21;
console.log(a);

If data types are not of a reference-type, called value-type, (which primitive values are in JavaScript), then mutability doesn't matter because it would be indistinguishable from immutability. Consider the following hypothetical scenario with a mutable, value-type number:

var a = MutableNumber(42);
var b = a; // creates a copy of MutableNumber(42) because it's a value type
a.add(1);
console.log(a, b); // would log 43, 42

In this scenario it is not possible for two variables to refer to the same mutable number value, a.add(1) is indistinguishable from assigning a new value to a (i.e. a = a + 1).

like image 76
Felix Kling Avatar answered Oct 15 '22 09:10

Felix Kling


I could understand your question , the thing is , inside the while loop , everytime the x will point to the new value , whereas the old value will be made ready for garbage collection , so the memory is mantained still.

Read this for better understanding : https://developer.mozilla.org/en-US/docs/Glossary/Mutable

So about the mutablity , your understanding is correct , the variable references the new value , the old value isn't changed , hence primitive values are immutable.

Refer: https://developer.mozilla.org/en-US/docs/Glossary/Primitive

like image 39
CHECoder08 Avatar answered Oct 15 '22 10:10

CHECoder08