37

I was reading the re-introduction to JavaScript on MDN and in the section Numbers it said that you can convert a string to a number simply by adding a plus operator in front of it.

For example:

+"42" which would yield the number output of 42.

But further along in the section about Operators it says that by adding a string "something" to any number you can convert that number to a string. They also provide the following example which confused me:

"3" + 4 + 5 would presumably yield a string of 345 in the output, because numbers 4 and 5 would also be converted to strings.

However, wouldn't 3 + 4 + "5" yield a number of 12 instead of a string 75 as was stated in their example?

In this second example in the section about operators wouldn't the + operator which is standing in front of a string "5" convert that string into number 5 and then add everything up to equal 12?

SineLaboreNihil
  • 953
  • 4
  • 11
  • 18

8 Answers8

52

What you are talking about is a unary plus. It is different than the plus that is used with string concatenation or addition.

If you want to use a unary plus to convert and have it added to the previous value, you need to double up on it.

> 3 + 4 + "5"
"75"
> 3 + 4 + +"5"
12

Edit:

You need to learn about order of operations:

+ and - have the same precedence and are associated to the left:

 > 4 - 3 + 5
 (4 - 3) + 5
 1 + 5
 6

+ associating to the left again:

> 3 + 4 + "5"
(3 + 4) + "5"
7 + "5"
75

unary operators normally have stronger precedence than binary operators:

> 3 + 4 + +"5"
(3 + 4) + (+"5")
7 + (+"5")
7 + 5
12
chamini2
  • 2,820
  • 2
  • 24
  • 37
epascarello
  • 204,599
  • 20
  • 195
  • 236
  • So does that mean that when a string is combined with other numbers it converts all other numbers to strings except when I specifically put another + operator in front of it (essentially two + operators (+ + "5")) and then and only then, JavaScript would interpret what's inside of a string as a number? – SineLaboreNihil May 13 '13 at 13:03
  • @SineLaboreNihil In `(+ + "5"))` first is for `add` second used to typecase `"5"` string to `5` int ..read the link – Grijesh Chauhan May 13 '13 at 13:09
  • @epascarello I get the order of operations I was just confused by the following sentence on MDN website: "If you add a string to a number (or other value) everything is converted in to a string first." It doesn't say anything about the order in which it is converted. Additionally the following sentence added to the confusion: "Adding an empty string to something is a useful way of converting it." It doesn't say that it would convert everything to a string only after it is detected as a string by JavaScript and that everything before it would remain intact. Anyways, thanks for your help. – SineLaboreNihil May 13 '13 at 13:20
  • Minus, on the other hand isn't so special. So `"2" - 1` returns `1`, but `"2" + -1` returns `"2-1"`. Debug your heart out! :P – poshest May 20 '15 at 23:04
  • @poshest - actually, `"unary -"` behaves exactly the same as `"unary +"`. `"2" + +1` would convert the number `+1` to a string (therefore `"1"`), then concatenate the two strings. Your example converts the number `-1` to a string (therefore `"-1"`), then concatenates the two strings. It is quite consistent. – ToolmakerSteve Aug 13 '20 at 23:12
  • @ToolmakerSteve Well, sort of. My example highlights the difference between addition `+` which can coerce the operands from numbers to strings (as Moritz's answer here makes explicit) compared to minus `-`, which cannot. I was not seeking to point out any inconsistency in `unary +` vs `unary -`. As you said, there isn't one. – poshest Aug 26 '20 at 13:04
  • @poshest - ahh, I see your point now. Thanks. The comparison that would have made it clear to me what you were saying is `"2" - 1` => `1` but `"2" + 1` => `"21"`. The use of `-1` (unary minus) in your second snippet isn't relevant to the point, IMHO, and threw off my understanding. Your point is simply that `-` doesn't have the "operator ambiguity" that `+` does. – ToolmakerSteve Aug 26 '20 at 23:53
  • @ToolmakerSteve I think my point back then was that I was confused. Much clearer now having re-examined in light of your comment. So thank you! – poshest Aug 28 '20 at 11:01
6

You could also use parseInt() or parseFloat(), like this:

> 1 + 2 + "3"
"33"
> 1 + 2 + parseInt(3)
6

I think that's alot cleaner than using +"3", but that is just my opinion.

Anickyan
  • 404
  • 2
  • 10
4

The answer can be found in Ecma262.pdf section 11.6.1:

If Type(lprim) is String or Type(rprim) is String, then a. Return the String that is the result of concatenating ToString( lprim) followed by ToString(rprim).

So that will resolve all operations according to precedence, so that as soon the string is found any number, the number is converted to string.

4 + 3 + "5"
"75"
4 + 3 + "5" + 3
"753"

To read the whole standard, go here.

Leponzo
  • 624
  • 1
  • 8
  • 20
Agus
  • 1,604
  • 2
  • 23
  • 48
3

When you look at Step 7 and "Note 2" at The Addition operator ( + ) (§11.6.1) in the ES5 specs,

it says

If Type(lprim) is String` or Type(rprim) is String, then Return the String that is the result of concatenating ToString(lprim) followed by ToString(rprim)


NOTE 2 Step 7 differs from step 3 of the comparison algorithm for the relational operators (11.8.5), by using the logical-or operation instead of the logical-and operation.

Meaning if either 7 (3+4) or "5" (|| not &&) is typeof "string" toString() is applied to both operands.

So the addition is actually applied to

"7" and "5" -> "7" + "5" //"75"

Community
  • 1
  • 1
Moritz Roessler
  • 8,542
  • 26
  • 51
2

A smple + operator in javascript is used for concatenation and not to add.

A bracket and a + operator before the string format integer variable will do the job every time.

This always works fine.

1 + (+"2") = 3

Because placing + before string converts the string variable into number.

fiddle here: http://jsfiddle.net/xuVur/2/

Sanjeev Singh
  • 3,976
  • 3
  • 33
  • 38
1

In the example of 3 + 4 + "5" - the Javascript parser will do (3+4)+"5", as the 3+4 is first - so it will add 3 + 4 because they are numbers, then concatenate the string "5".

Modul8
  • 46
  • 2
  • This is what's confusing me and I'm pasting it here as it is written on the MDN website: "If you add a string to a number (or other value) everything is converted in to a string first."... So apparently this only works if you add a string first, before the numbers. If you add it after the numbers the numbers will add up (plain math) and then concatenate a string to it afterwards. Meaning that in this case everything won't be converted to a string. – SineLaboreNihil May 13 '13 at 13:08
  • 1
    Not technically correct, as although Javascript isn't typed, it will still preserve types unless you convert, such as int->string, string->int. – Modul8 May 13 '13 at 13:17
1

When you add a number and a string in Javascript the result is always a string.

Check the experiment here - http://jsfiddle.net/xuVur/1/

var a = "3" + 4 + 5;
var b = 3 + 4 + "5";

$("#result1").html(a); // prints 345
$("#result2").html(b); //prints 75
rahul maindargi
  • 5,359
  • 2
  • 16
  • 23
UnTechie
  • 365
  • 2
  • 7
0

It is confusing, but binary + seems to be a rare case of a number being coerced to a string. In most cases it would be the other way round (so it's very easy to think that you don't need to be too fussy about the type)

4+"3" = "43"

4-"3" = 1

quantropy
  • 101
  • 1