最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How a string(like "123") is converting to a number if I add + before it but not after it? - Stack

programmeradmin1浏览0评论

When I add a + before a number in quotes like +"123", it is converting to typeof number but if I add like "123"+, it is waiting for next operands. Why? Why in the first case it is converting to a number?

When I add a + before a number in quotes like +"123", it is converting to typeof number but if I add like "123"+, it is waiting for next operands. Why? Why in the first case it is converting to a number?

Share Improve this question edited Jan 5, 2017 at 10:33 Holt 37.7k7 gold badges98 silver badges144 bronze badges asked Jan 5, 2017 at 10:30 Ram_TRam_T 8,49411 gold badges41 silver badges63 bronze badges 3
  • 7 Because there is a + unary operator, just like there is - one - You can do -1 or +1, but you cannot do 1+, it doesn't make sense. – Holt Commented Jan 5, 2017 at 10:33
  • Why it is converting to a typeof number? That is the actual question – Ram_T Commented Jan 5, 2017 at 10:34
  • 1 because operators will cast their operands to a type that makes sense. Quoting the spec : "The unary + operator converts its operand to Number type." – Aaron Commented Jan 5, 2017 at 10:36
Add a ment  | 

4 Answers 4

Reset to default 8

In the first case, you use an Unary plus +

The unary plus operator precedes its operand and evaluates to its operand but attempts to convert 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.

in the second you are using just an Addition

The addition operator produces the sum of numeric operands or string concatenation.

+"123" can be parsed as the unary + operator followed by the String 123. As you can see in the spec, the job of the unary + operator specifically is to cast its operand to a Number type.

"123"+ can't be parsed as a valid JS expression, because the + token should always be followed by an operand, whether it is to be parsed as the unary + operator or the binary + addition operator.

As you have + and - symbols(as unary operators) to numbers prepended only not appended. In the same way, when you add some + or - to strings, same applies and are if able to convert to numbers they will be converted to numbers. But when you append that, it is treated as binary operator. So, concatenates.

You can understand it by reading this article https://developer.mozilla/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators

Unary operator. Attempts to convert the operand to a number, if it is not alread

发布评论

评论列表(0)

  1. 暂无评论