3
return "1";  // returns string
return +"1"; // returns int

I'd like to know what this method is called when using +"n" to convert from string to int.

2
  • because in this particular case, the + is being interpreted as "make this value positive"-type operation. a "negative string" doesn't exist, so the string-1 gets coerced to an int. it'd be no different if you had return -"1", except you'd get a negative back instead Commented Mar 13, 2015 at 17:24
  • Tried adding a +"1" comment, got rejected by the quality filter. Commented Mar 13, 2015 at 17:27

4 Answers 4

5

It's just the intrinsic type coercion that the unary + operator performs according to the language spec.

  1. Let expr be the result of evaluating UnaryExpression.
  2. Return ToNumber(GetValue(expr)).

So although conceptually it's just the opposite of unary -, since a + operation just means "multiply by positive 1" the only real work that's done is to coerce the value to a number.

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

Comments

4

From MDN:

Unary plus (+)

The unary plus operator precedes its operand and evaluates to its operand but attempts to converts it into a number, if it isn't already. Although unary negation (-) also can convert non-numbers, unary plus is the fastest and preferred way of converting something into a number, because it does not perform any other operations on the number. It can convert string representations of integers and floats, as well as the non-string values true, false, and null. Integers in both decimal and hexadecimal ("0x"-prefixed) formats are supported. Negative numbers are supported (though not for hex). If it cannot parse a particular value, it will evaluate to NaN.

Examples:

+3     // 3
+"3"   // 3
+true  // 1
+false // 0
+null  // 0

4 Comments

This is lifted from developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…, but with the sentences separated into paragraphs... for some reason.
Plagiarized from developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… .. copied text should be placed within blockquotes, and you should link to the source
Don't try to sidestep the issue. You need to cite your sources. That is a rule. See meta.stackexchange.com/questions/160077/…
@CatalinMunteanu no that's not the problem I think, juste cite the sources, it gives the impression you try to somehow hide this information, even though it is probably not the case
1

You are using the unary operator + which tries to cast anything to its right to a Number (as a float).

Note that it may return NaN if it can't be cast to a Number.

Similarly you can use parseFloat, which may return NaN as well

parseFloat(anyValue, 10); 

Comments

0

You're applying the unary operator +.

It's not defined for string, so JS has to convert the string to a number first.

Of course, be careful not to accidentally turn it into a binary operator. It's not something you want to use in real code, because it's kind of fragile :)

Comments

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.