11

Does the addition operator before char in javascript convert them to number?

1 + + "1" === 2; 
+"1" + + "1" === 2;
"1" + "1" ===  "11"

Earlier question doesn't explain why it's happening only tells us various ways of converting strings to number and vice versa.

vipin goyal
  • 671
  • 11
  • 17
  • 2
    [MDN: Operator precedence](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence), [Unary plus (+)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators#Unary_plus) – Andreas Aug 26 '17 at 07:32
  • 2
    This is specified in ECMAScript. Type conversions should be as weird, implicit and unexpected as possible. – Eric Duminil Aug 26 '17 at 14:09
  • Please read the [documentation](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators#Unary_plus_()). –  Aug 26 '17 at 15:01
  • @torazaburo: It's not easy to find this documentation: OP should understand first how the syntax is parsed and that `+ "1"` is called unary plus. – Eric Duminil Aug 26 '17 at 17:44
  • @EricDuminil Google for "addition string javascript". The first result is the SO dup. –  Aug 26 '17 at 18:03
  • @torazaburo The linked answer doesn't look like a corresponding duplicate to me. It doesn't explain `+ "1"` being converted to `1` for example – Eric Duminil Aug 26 '17 at 18:12
  • The third answer explains that. –  Aug 26 '17 at 18:19
  • @torazaburo I might be nitpicking, but the third answer doesn't explain anything. It merely uses unary plus for fast conversion. There must be a better dupe. – Eric Duminil Aug 27 '17 at 07:54
  • @EricDuminil Right, that dup target is not perfect. Feel free to suggest another one. –  Aug 27 '17 at 10:11
  • *Earlier question doesn't explain why it's happening only tells us various ways of converting strings to number and vice versa.* Huh? Explaining that `+ "1"` is a way to convert a string into a number, because that's the spec, is 100% identical to explaining why it happens. Even if that dup target doesn't happen to be perfect, this kind of behavior has been documented in hundreds or thousands of tutorials, intros, blog posts, and documentation pages over the last several decades. –  Aug 28 '17 at 06:33

4 Answers4

16
1 + + "1" === 2; 

Unary operator + has higher precedence, so +"1" will be evaluated first, converting "1" into integer of value of 1, so it will become

1+1 === 2

The second line

+"1" + + "1" === 2;

This is similar. Unary operator + has higher precedence, so both +"1" will be evaluate to positive integer value of 1.

"1" + "1" ===  "11"

Because in JavaScript + is also string concatenation operator if both operands are string, this will concat both strings.

More information https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators

Update: @torazaburo is correct.

  • The evaluation of +"1" in code here has nothing to do with operator precedence.

  • String concatenation will happen if either operand is string.

Zamrony P. Juhara
  • 5,222
  • 2
  • 24
  • 40
  • 1
    This is wrong on a number of levels. First, applying the unary `+` operator is not a matter of **precedence**; it is the only way to parse the expression. "Precedence" refers to the rules for evaluating something like `a + b * c`. Second `+` is not the string concatentation operator if **both** operands are strings; it concatenates if **either** is a string. –  Aug 26 '17 at 14:57
  • You are correct. – Zamrony P. Juhara Aug 26 '17 at 22:26
3

What you are looking for is the unary plus. Lets analyze the first 2 cases:

The unary plus operator precedes its operand and evaluates to its operand but attempts to convert it into a number, if it isn't already.

1 + + "1" === 2

+ "1" part is converting the string "1" to a number 1. The number 1 is then added to the other number 1 resulting in 2 === 2

+ "1" + + "1" === 2

Same as above, but instead there are 2 unary operation now. So both string "1" are converted to number 1 resulting in 2 === 2

lap00zza
  • 105
  • 1
  • 10
2

In JS there is implicit type coercion. For instance, the binary plus operator coerces a number into string if the other operand is a string. "1" + 2 === "12" or 3 + "4" === "34".

However when used with a single operand it works backwards and +"1" coerces the "1" string to a number 1. Just like parseInt("1").

So +"1" + 3 === 4

Redu
  • 25,060
  • 6
  • 56
  • 76
-1

The first 2, you are casting to number by prefixing with +.

The last one is just concat of 2 strings, by default, js will cast up, and it will cast number to string in 1 + "1" === "11"

felixmosh
  • 32,615
  • 9
  • 69
  • 88
  • The term "cast" is not used in JavaScript. –  Aug 26 '17 at 15:03
  • @torazaburo, why not? – felixmosh Aug 26 '17 at 17:00
  • "Cast" typically refers to a particular operation in a typed language where the programmer is forcing a particular type. Since JS doesn't have types, it doesn't have casting. Sometimes it itself will convert types, which is called type conversion, or coercion. –  Aug 26 '17 at 18:04