immutability - Why number are immutable in Javascript? - Stack Overflow

I have read the question and answer here:javascript numbers- immutableBut it's not enough clear fo

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.

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.

Share Improve this question asked Sep 24, 2017 at 16:33 JamoJamo 5045 silver badges27 bronze badges 7
  • 5 What would it mean if the number 5 were mutable? – Pointy Commented Sep 24, 2017 at 16:35
  • Reading the answer in the question you linked: The numbers themselves are immutable (or you could say constants). The variable x is a reference to a number/constant. It also says there that it is the same in all programming languages. Otherwise you could remove/overwrite numbers and mess up the whole math/calculations that the language can do. Right? – Krisztián Balla Commented Sep 24, 2017 at 16:40
  • Mmm but if the number are somewhat constants why we can reassgin them values? x= 5 then x=1. – Jamo Commented Sep 24, 2017 at 16:46
  • 3 You can't. x is a variable, 5 and 1 are numbers. Numbers are immutable, variables declared with keywords that allow reassignment (in current JS, var and let) are not. As for "is it efficient?": yes. Processors understand numbers at the hardware level. And your code has the same reference x to a different number at each iteration. – Mike 'Pomax' Kamermans Commented Sep 24, 2017 at 16:59
  • 2 there are no references to primitive numbers. those numbers are stored as variable value always in the same place in memory. – Marcin Malinowski Commented Sep 24, 2017 at 21:13
 |  Show 2 more ments

5 Answers 5

Reset to default 3

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 posed 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).

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/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/en-US/docs/Glossary/Primitive

Mutation is a change of state while numbers (the primitive type) are pure state objects. Any mutation to such a "object" state is actually a new number. Number itself is like an identifier of a mutation of bits in a cell of puter memory.

Therefore numbers are not mutable. Same as colors or characters.

It is also worth claryfining that a new number will ocupy the same memory cell as the old one for any given variable. Actually replacing the old one. Therefore there is no performance hit of any kind.

i dont understand entirely this,

var a=12;
a=45;

we can infer from ;

1.firstly interpreter allocate a chunk of memory and locate 12 to this area. actually (a) is label of this memory area. 2.than, interpreter allocate a new memory cell and locate 45 to this area. lastly (a) is associated with this new memory cell. The memory cell that contain 12 is destroyed by garbage collector.

Primitive values vs. references:

In JavaScript values of type boolean, undefined, null, number, symbol, string are primitive. When you assign them to a variable or pass them as an argument to a function, they always get copied. In contrast objects have two parts to them: Object (it's data) is stored in one chunk of memory and what you assign to a variable is a reference pointing to that object. Object itself is not copied on assignment.

Because numbers are always copied when you assign them to a variable it is impossible to change a number and see that change through some other variable without actually assigning a new value to that variable. In contrast when you change a field on an object, all variables pointing to that object will see that change.

Let's see some examples:

var a = 1;
var b = a; //This creates a new copy of value 1
console.log(a, b); // 1 1
a = 2;
console.log(a, b); // 2 1


var obj_a = {attr: 1};
var obj_b = obj_a; //This makes a new reference to the same object.
console.log(obj_a, obj_b); // {'attr': 1} {'attr': 1}
obj_b.attr = 2;
console.log(obj_a, obj_b); // {'attr': 2} {'attr': 2}

Immutable objects: immutable.js

Libraries like immutable.js provide types that bine properties of primitive types and objects: Types from immutable.js behave as ordinary objects as long as you don't change them. When you change a property of an immutable object, a new object is created and the change is only visible through that new object. The benefit is that you save space in memory and you can quickly check equality of objects by just paring their references. You get a set of types that behave like integers but you can store more plex structures in them.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745330671a4622862.html

相关推荐

  • immutability - Why number are immutable in Javascript? - Stack Overflow

    I have read the question and answer here:javascript numbers- immutableBut it's not enough clear fo

    6小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信