0

I am new to javascript and I am a little confused with the concept of immutability. Are javascript primitive variables immutable because they are accessed/ passed by value ? If this isn’t the reason, please mention.

7
  • 1
    They are immutable by design. There does not have to be a reason. Other languages are based on different decisions. Commented Jul 25, 2021 at 16:43
  • Also related: Why can't I add properties to a string object in javascript? Commented Jul 25, 2021 at 16:45
  • "Are primitive variables immutable because they are accessed/passed by value?" - no. Everything in JavaScript is passed by value, there are no function calls with references to variables. Commented Jul 25, 2021 at 17:21
  • @trincot I've never seen languages where primitive values are mutable, tbh. (Uh, Java's integer wrappers can be mutated with reflection, but that doesn't count as design). The only decision that really distinguishes languages here is whether strings are primitives or not. Commented Jul 25, 2021 at 17:56
  • @Bergi but objects are passed by reference. So that’s why they are mutable ? Commented Jul 25, 2021 at 18:27

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.