1

In JavaScript, strings are immutable. That means any operation on them returns a new object. Methods like trim, replace and slice don't modify the existing string.

However, I was playing around in jsconsole and found an exception

string = "string";
string + 37;
=> "string37"
string;
=> "string"

The original string hadn't changed here. Here's what happens when I apply the increment operator on the string. Here I am expecting to see string returned.

string++;
=> NaN
string
=> NaN

I was trying to see whether this would return strinh, like it would in some other languages.

Regardless of whether string++ would work, it shouldn't modify existing string objects. Yet it did just that. Does anyone know why?

0

2 Answers 2

4

That ++ operator works similarly to:

string = string + 1;

So, you are re-assigning to that variable. (while the original string remains immutable)

If you run string = string + 1 yourself, you'll notice it results in string1. This is because the 1 is being converted into a string and then treated as an "append". (again, never modifying the original string value)

However, the ++ does not attempt to coerce anything, it simply attempts to add a non-number (ie: string) to a number (1) and the result is NaN.

Sign up to request clarification or add additional context in comments.

4 Comments

A number added to a string returns a string. In my case, string = string + 1 and string += 1 both return string1. string++ returns NaN. If I call string again, NaN appears. This means the string was modified
@user4703663 The string was not modified, but the variable now contains a different value.
Well, the ++ operator isn't completely identical to that code. string + 1 ends up casting 1 as a string, appending to the original and returning a new value. The ++ doesn't perform that casting, instead it tries to add a non-number to a number, resulting in NaN
Regardless, the point is that the string variable simply contains a new value, but the original string value was never mutated.
3

Here is a simple example:

var baz = "string";
baz = 5;

Certainly we have not modified the value of the string "string" by assigning baz to 5. Instead, we have simply done away with the string altogether and put the value 5 in its place. Similarly, your example does not alter any strings by assigning your variable to NaN.

You expect the variable string to be a constant, but it's not. Furthermore, constant variables and immutable values are different things. You haven't changed the immutable string "string" into NaN; you have substituted the immutable value "string" for a completely different value, NaN.

Your operations create new values by using the immutable string as an operand.

Consider a real constant variable in an environment that supports the const keyword:

> const foo = 5;
> foo++
  5

Now consider a constant variable that is an object:

> const bar = {};
> bar.baz = 5
> bar
  { baz: 5 }
> bar = 10;
> bar
  { baz: 5 }

In this case, we mutated the value (because the value is not immutable) but could not alter which value the variable contains. Your example does not use constant variables, so the variable string can freely change its value.

1 Comment

This answer is perfectly explained. I will accept this

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.