2

Why does this JavaScript statement:

console.log(1 +  + "2");

print

3

as the output? I am not sure why it's printing this - I expected "12".

jdphenix
  • 15,022
  • 3
  • 41
  • 74
Siva
  • 113
  • 1
  • 2
  • 7
  • 1
    Why don't you run the code yourself? Then you can still ask for an explanation. What in particular are you confused about? – Felix Kling Feb 13 '15 at 06:03
  • 1
    @CoDeMurDeRer No, it's "valid" JavaScript – jdphenix Feb 13 '15 at 06:05
  • http://stackoverflow.com/q/9081880/645956 – grc Feb 13 '15 at 06:06
  • `+` or `-` operand in front of a string converts it to number. so here `+"2"` will become `2` and then `1 + 2` which will be `3`. – Mr_Green Feb 13 '15 at 06:07
  • they edit question... first was like `console.log(1 + + ""2);` so i said that it will give error... – coDe murDerer Feb 13 '15 at 06:07
  • Hi @FelixKling, i can run the code, i know that. it is printing 3, i don't know how it is printing 3, that's why i asked. you are not talented guy when u was born. – Siva Feb 13 '15 at 06:11
  • *"it is printing 3, i don't know how it is printing 3, that's why i asked"* Then why didn't you just ask that instead of asking what the output is? As I said, asking for an explanation is perfectly fine. Asking for the result of one line of code is not. Also, I assume you must have put *some* thought into why the result is 3. What else would you have expected? If you explain your thoughts we can provide better answers. – Felix Kling Feb 13 '15 at 06:13
  • @Siva you misunderstood felix. you didn't explain your question clearly so he asked like that. anyway don't mind the votes. – Mr_Green Feb 13 '15 at 06:13

4 Answers4

10

+ or - operand in front of a string converts it to number. so here +"2" will become 2 hence the result will be 3.

=> 1 + + "2"    // +"2" = 2
=> 1 +    2
=> 3

If you use - in between like

=> 1 - - "2"   // -"2" = -2
=> 1 - - 2     // 1 - (-2)
=> 1 + 2
=> 3

So,

     -"2" ==> -2
     +"2" ==>  2
 +"Hello" ==> NaN
 -"Hello" ==> NaN
Mr_Green
  • 40,727
  • 45
  • 159
  • 271
  • Could you explain why `1 + + "2"` transformed into `1 + 2` where as `1 - - "2"` into `1 - - 2`? I am bit confused about it. –  Feb 13 '15 at 07:06
  • @TamilVendhanKanagaraju I updated the post. I hope it is now clear. – Mr_Green Feb 13 '15 at 07:09
  • Its clear now, but, I am still not able to understand that when `-"2"` is transformed into `-2`, why `+"2"` is not transformed into `+2`. –  Feb 13 '15 at 07:31
  • @TamilVendhanKanagaraju `+2` will be automatically become `2`. You will not write `+1 + 2` while calculating. you will just write `1 + 2`. – Mr_Green Feb 13 '15 at 07:42
  • 1
    @Tam `-` in `-"2"` acts as a [unary negation operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators#Unary_negation_(-)) which negates the converted number 2 which is positive (+2). So, it goes like that: `-"2"` -> `-(+2)` -> `-2`. – cychoi Feb 13 '15 at 07:43
  • @cychoi: [clap]. thanks everyone. i am clear now. thanks for your time! –  Feb 13 '15 at 10:34
1

console.log(1 + "2") prints 12 as + acts as an concatenation operator.

But if you try to print console.log( + "2" ) you will get output as 2 coz it is casted as an integer.

Therefore console.log( 1 + +"2" ) will give you result as 3

Chirag Swadia
  • 1,099
  • 1
  • 9
  • 24
1

Regarding the specific output of

console.log(1 +  + "2");

Run it on your browser console. The better question is why does it output what it does -

console.log(1 +  + "2");
              ^

That is the binary + operator, which will concatenate strings or add numbers.

console.log(1 +  + "2");
                 ^

That one is the unary + operator, which converts "2" to a number.

Don't create JavaScript like this. It's confusing.

jdphenix
  • 15,022
  • 3
  • 41
  • 74
-3

console.log(1 + "2"); This will give concated output 12

  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/34723595) – roapp Jul 25 '23 at 05:52
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 25 '23 at 11:24