5

Everybody knows the basic concatenation of two strings in JavaScript:

> "Hello " + "World!"
'Hello World!'

But what happens if we use + + instead of +? I just encountered the following weird behavior:

> "Hello " + + "World!"
'Hello NaN'
> "Hello " + + ""
'Hello 0'

From the examples above, I can see that the second string is converted into number. So, passing an object having valueOf property as function, the value returned by that function will be converted.

> "Hello " + + ({valueOf: function () {return 1; }})
'Hello 1'

As expected, it shows "Hello 1".


Why is the second string converted in Number? Why not throwing a syntax error or so?

3
  • @jeremyharris Yep, but not + + (+ <space> +). Commented Aug 27, 2014 at 17:56
  • evaluating + "" gives you zero Commented Aug 27, 2014 at 17:57
  • at all: got it, deleted :) Commented Aug 27, 2014 at 17:57

3 Answers 3

3

The second + is the unary plus operator whose purpose is to convert its operand to a number.

The unary plus operator precedes its operand and evaluates to its operand but attempts to converts it into a number, if it isn't already. Although unary negation (-) also can convert non-numbers, unary plus is the fastest and preferred way of converting something into a number, because it does not perform any other operations on the number. It can convert string representations of integers and floats, as well as the non-string values true, false, and null. Integers in both decimal and hexadecimal ("0x"-prefixed) formats are supported. Negative numbers are supported (though not for hex). If it cannot parse a particular value, it will evaluate to NaN.

"Hello " + + "World!"

can only be parsed as

"Hello " + (+ "World!")

which is

"Hello " + NaN

hence your result.

This unary operator is very useful in JavaScript and is one of the most common ways to convert from something which may be a number or a string to a number. It also has the advantage over parseFloat that it's not ridiculously tolerant (parseFloat("7up") would return 7) which makes it an easy way to see if a string is the representation of a number (just test s==+s).

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

2 Comments

Indeed, can you explain why isn't it a syntax error?
Why would it be a syntax error ? +somestring is perfectly valid (see the doc I linked to)
2

Great question. This is because JavaScript has a unary + operator (similar to the unary - as in, ex, -1, which is parsed to - (1)): https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators#Unary_plus_(.2B)

Thus "foo" + + "bar" is parsed to "foo" + (+ "bar") (or if you prefer a lisp-style parse tree: (+ "foo" (+ "bar")).

The unary + converts its operand to a number, or NaN if the operand isn't a sensible number:

> +"42"
42
> +"foo"
NaN

Finally, adding a number (including NaN) to a string results in a string concatenation.

Thus "foo" + +"bar" can be expanded:

"foo " + +"bar"
"foo " + NaN
"foo NaN"

2 Comments

No, it's parsed as ("foo" + (+"bar"))
Err, sorry, using lisp-style syntax; Will clarify
1

Here + is a unary plus operator Which tries to convert the operator in number & works as follows

+"" //return 0
+"Some" //returns NaN

So here

"Hello " + + "World!" 

will be

"Hello " + (+ "World!")
"Hello " + NaN
"Hello Nan"

Comments

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.