2

When I have one plus, I get the wrong answer e.g.

var b = [069];
var total = 0;

total = total + b
console.log(total) // total = 069

However, when I add a second plus so the equation looks like this

total = total + + b  // total = 69

I get the correct answer of 69. The above is just a simplified example of my issue.

This works fine, however whilst using JSHint I get a warning saying

confusing pluses

How can I get the correct answer without using the + + ? Also, what is this operator called?

8
  • 2
    [069] is an array; you're initializing b to an array value, and then trying to use it with the + operator. What is it that you expect that to do? Commented Oct 17, 2016 at 19:35
  • 1
    Why not just var b = 69;? Commented Oct 17, 2016 at 19:36
  • 4
    + in front of a variable would cast it to a number if I'm correct. Try this in your console "5" would return "5", where +"5" would return 5. You could use total = parseInt(total) + parseInt(b); to get a correct result. Commented Oct 17, 2016 at 19:36
  • 1
    This seems to be perfect Answer material @AlexSzabó, go for it :) Commented Oct 17, 2016 at 19:39
  • 2
    You can also get the same answer if you try with total +- b. You will not need the space between operators if you are are using the +-. You will need the space as + + since ++ is considered to be an increment operator. Put a + or a - before a symbol to convert to an integer. Commented Oct 17, 2016 at 20:20

2 Answers 2

9

Javascript unfortunately does a lot of implicit conversions... with your code

b + [69]

what happens is that [69] (an array containing the number 69) is converted to a string, becoming "69". This is then concatenated to b that also is converted in this case to a string "0". Thus the result "069".

If however you add another unary + in front of the array the string gets converted back to a number and you get a numeric result added to b.

0 + [69] → 0 + "69" → "0" + "69" → "069"
0 + + [69] → 0 + + "69" → 0 + 69 → 69

Exact rules are quite complex, but you can be productive with Javascript just considering the simplified form that for binary +:

  • if both are numbers the result is numeric addition
  • otherwise they're converted both to string and result is concatenation

One thing that is somewhat surprising is that implicit conversion of an array to a string is just the conversion of elements to string adding "," between them as separator.

This means that the one-element array [1] is converted to "1"... and implies apparently crazy consequences like [1] == 1.

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

13 Comments

"unfortunately" You might want to de-bias your answer :P
@EmileBergeron how advanced the answer has nothing to do with how skilled OP is. They asked a question, they got an answer.
that is irrelevant.
Did you just assume OPs knowledge?
@EmileBergeron unfortunately that really doesn't matter. It's a very correct answer to OPs question. You can't reduce an answer, potentially making it wrong in order to cater to OP.
|
4

Posting my comment as an answer

+ in front of a variable would cast it to a number if I'm correct.

Try this in your console:

"5" would return "5" (string), where

+"5" would return 5 (number).

You could use total = parseInt(total) + parseInt(b); to get a correct result, as parseInt() would try to make a number out of any input parameter it gets.

Theoritecally, you could just parse the total as a number, but it would be prone to an error like "1" + "0" = "10" resulting in 10, which should mathematically be 1.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.