JavaScript adding a string to a number

83,792

Solution 1

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

Solution 2

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.

Solution 3

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.

Solution 4

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"

Solution 5

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/

Share:
83,792
SineLaboreNihil
Author by

SineLaboreNihil

Computer enthusiast, eager to learn.

Updated on October 22, 2020

Comments

  • SineLaboreNihil
    SineLaboreNihil over 3 years

    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
    SineLaboreNihil almost 11 years
    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
    SineLaboreNihil almost 11 years
    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.
  • Grijesh Chauhan
    Grijesh Chauhan almost 11 years
    @SineLaboreNihil In (+ + "5")) first is for add second used to typecase "5" string to 5 int ..read the link
  • rahul maindargi
    rahul maindargi almost 11 years
    Your are printing a always
  • UnTechie
    UnTechie almost 11 years
    My bad. I changed that now.
  • Modul8
    Modul8 almost 11 years
    Not technically correct, as although Javascript isn't typed, it will still preserve types unless you convert, such as int->string, string->int.
  • SineLaboreNihil
    SineLaboreNihil almost 11 years
    @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.
  • poshest
    poshest almost 9 years
    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
  • ToolmakerSteve
    ToolmakerSteve over 3 years
    @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.
  • poshest
    poshest over 3 years
    @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.
  • ToolmakerSteve
    ToolmakerSteve over 3 years
    @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.
  • poshest
    poshest over 3 years
    @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!