1

By this I mean NOT the default javascript behavior of string + number = string, or string + string = string. From http://www.javascriptkit.com/jsref/arithmetic_operators.shtml, the Unary plus will convert a string to a number.

So how would I specify that I want numeric addition with two strings? This seems to work, but is somewhat ugly:

var a = "5";
var b = "2";
var c = +a + +b;

Does there exist a (numeric only +) operator, that always returns a number? Can one be defined like in other languages? For examples, perhaps '%%' or '+^' or '+++'? Or is this just not possible?

1 Answer 1

1

No. If you want to perform numeric addition than you have to ensure that both sides of the expression are Numbers before you start.

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

1 Comment

No there isn't one and No you can't define your own operators (like in C# or Java for example)?

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.