Hacker News new | ask | show | jobs
by zarathustreal 2150 days ago
Yes precisely this. The person above you might be unaware of the fact but JavaScript (and all other languages that I’m aware of) does indeed make use of pointers albeit indirectly. When you use const and initialize it as a JS object the value in the variable is a “pointer” (call it what you will, it points to or references a memory location) and is indeed immutable. You cannot change the value of the variable. You can, however, change the value of a different variable (such as a property of the object it references) but at that point you’re not longer talking about the same variable. So in a sense “no, it’s not immutable” but in another sense, yes it actually is. It makes the most sense to talk about the value that the variable holds rather than the object it references when speaking of immutability because there also exist primitive values in JS that are immutable in exactly the same way, except they are not references (or pointers) they are just values. So instead of needing to keep the additional “gotcha, object references are immutable but the properties of that object are not frozen” you can simply understand the literal semantics. Of course that is just my personal opinion
1 comments

You're missing the forest for the trees.

Do you think your 200-word explanation is easier for a beginner to understand than "this value is immutable — it will never change"?

My initial argument was that JavaScript is a terrible choice for learning FP, precisely because it has so many caveats and is notoriously poorly understood, even by people who use it every day. Your lengthy explanation proves my point.

I find this argument a bit disingenuous to be honest, I took the liberty of spelling things out in my reply because I assumed you wouldn’t otherwise be able to see the point. An explanation to a beginner, which doesn’t require memorizing any additional gotchas, would be something like “variables hold values. Strings, numbers, booleans, and references are values. Variables declared with const contain values that are immutable. An object is not a value.”
Likewise, I think it's disingenuous to suggest that "an object is not a value" is not an "additional gotcha".

Differences in evaluation strategies is not really beginner material.