0

Why does this

2 + + 3

return 5, but this

'2837363' + + '/'

returns

"2837363NaN"? Even the '/' got lost.

Why would a programming language accept this syntax without throwing a syntax error? When does it assume, the empty place evaluates to 0 (1st example) and when to NaN (2nd example)?

1
  • 2
    Aaahh... javascript :) You should love it to understand it. Commented Aug 6, 2015 at 10:51

1 Answer 1

4

Prepending a variable with a + implies type coercion to a number type.

+ 5 => 5
+ '5' => 5
+'a' => NaN
+'/' => NaN

When you do +'/' the result is NaN

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

5 Comments

You're right, now I can see: 2 + + 3 evaluates to 2 + (+3), 2nd example similarily. Thanks
That's called Unary plus
I know the unary plus, but I didn't recognize it here. I thought, my expression would be something like 2 + SOMETHING + 3 but of course it isn't, which now I can see.
It converts the value to a number, not an int specifically.
+'0.1322' => 0.1322 your right, thanks for pointing out @FelixKling

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.