JavaScript String Concatenations A Few Gotchas

Oct 29, 2017
>>>

We use string concatenations in JavaScript frequently. But if we look closely, it may raise a few new confusions. So, let’s begin.

What will be the output of 'a' + + 'b'. aNaN. It’s understandable, that something unexpected should come, as we’ve mis-typed an extra +, but what it has to do with the NaN. Why NaN?

Now, what will be the output of 'a' + + 'b' + 'c'. aNaNc. Quite understandable. Right?

Now, tell me, what will be the output of 'a' + + 'b' + 2. Obviously, aNaN2. Correct.

But, what will be the output of 'a' + + 2. Can you guess? Shockingly, but not so unexpectedly, it’s a2. We know, there is some relation of string concatenations with numbers. So, 2 has absorbed the NaN and hence the result. But what is the relation of string concatenation with Numbers. I’m a little confused. But I’ll try to find the answer.

Blog comments powered by Disqus