8

I come from c# background where immutable is achieved with public get ,private set properties. I have read that numbers in javascript are immutable so how can I do the following

var x = 6 / 2;
console.log(x);  // 3
 x = 8;
console.log(x); // 8

I have changed x, which I thought I couldn't?

Noel
  • 5,037
  • 9
  • 46
  • 69
  • 3
    Strings are immutable in c#. `var str = "hi"; str = "world";` Now `str` now is equal to `"world"` what happened? – asawyer Nov 23 '11 at 20:26
  • 1
    If numbers were mutable, you could have `y = x`, modify `x` and `x === y` would still be true. But you can't. Of course you can always change what `x` refers to. – Felix Kling Nov 23 '11 at 20:26
  • 3
    "public get, private set" has little to do with immutability. If it's immutable, there's no `set`. –  Nov 23 '11 at 20:33
  • delnan, If I have an object Car with a property colour, I can make the car immutable if I have a get\private set! – Noel Nov 23 '11 at 20:48
  • Checkout http://stackoverflow.com/questions/2326072/javascript-function-pointer-assignment Especially `Bob`'s answer. – P.Brian.Mackey Nov 23 '11 at 20:52
  • @Noel: Such an object is not immutable, it's only slightly less mutable. All code with can use the private setter (at least all methods defined in that class; probably more, but I'm not a C# expert) can mutate its colour property, and hence the object as a whole. –  Nov 23 '11 at 20:57
  • Thanks P.Brian, that explains. delnan yes the object itself can change it but only if your within the object. Nothing outside the object can make changes – Noel Nov 23 '11 at 21:11
  • @Noel And in doing such a change, would be considered a mutable object. That is his point. An immutable object does not change at all after being created, and in your car example, changing the color would result in a new seperate instance with the new color in addition to the old car object. – asawyer Nov 23 '11 at 21:13
  • @Noel: Doesn't matter. If an object, once fully constructed, changes (in an observable way - this clause just exists to allow some clever, but correct optimizations), it's not immutable. Yes, you can have immutable objects this way (you can even, contrary to my assertion, have an immutable object with internal mutability - but must not be observable). But a `private set` is a *far* shot from implying or even hinting at immutability. –  Nov 23 '11 at 21:14

2 Answers2

22

The numbers themselves are immutable. The references to them that are stored in the variable are not.

So 6 / 2 gets you a reference to the immutable 3, and then = 8 assigns a new reference to the immutable 8.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • 7
    And none of that is specific to JS. It applies to pretty much every language, including C#. –  Nov 23 '11 at 20:33
1

C# also allows a programmer to create an object that cannot be modified after construction (immutable objects). If you assign a new object in C# to an immutable object, like say a string. You are getting a new string rather than modifying the original.

What you demonstrated here isn't all that different. You can try a const instead

const x = 6 / 2;
console.log(x);  // 3
 x = 8;
console.log(x); // 3

Reference

Syntax

const varname1 = value1 [, varname2 = value2 [, varname3 = value3 [, ... [, varnameN = valueN]]]];

Browser compatibility

The current implementation of const is a Mozilla-specific extension and is not part of ECMAScript 5. It is supported in Firefox & Chrome (V8). As of Safari 5.1.7 and Opera 12.00, if you define a variable with const in these browsers, you can still change its value later. It is not supported in Internet Explorer 6-9, or in the preview of Internet Explorer 10. The const keyword currently declares the constant in the function scope (like variables declared with var).

P.Brian.Mackey
  • 43,228
  • 68
  • 238
  • 348
  • That isn't even valid JavaScript. It isn't valid C# either (lacking a type for `x`). And neither language accepts that snippet in its entirety and outputs what the comments indicate. What are you trying to show? Besides, you explanation seems to confuse objects with variables - please get that straight, your current explanation is *very* confusing! –  Nov 23 '11 at 21:18
  • My point is that both outcomes are possible in JavaScript. Sure, it's not straight ECMAScript. Tell me what web implementation follows standards specifications to the "T"? offline storage,javascript,html, Browsers? no,no,no,no. As web devs we have to think outside the box. Use the technology that is appropriate given the supported tools and the environment. – P.Brian.Mackey Nov 23 '11 at 21:59
  • 1
    I'll be bold and say that no (relevant) JavaScript implementation is *that* broken. There's no `const` keyword and no other way to silently skip(!) subsequent assignments. Or can you provide a reference for such a thing? -1 as I have to assume you're talking BS and beating around the bush when confronted with it. –  Nov 23 '11 at 22:04
  • Please excuse my ignorance and hash words; I removed the downvote. However, I think you should both add a disclaimer that it's not cross-browser and consider whether your example addresses the question. –  Nov 23 '11 at 22:07