nr: #1 dodano: 2017-01-05 11:01
As you have
- symbols(as unary operators) to numbers prepended only not appended. In the same way, when you add some
- 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.
nr: #2 dodano: 2017-01-05 11:01
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
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.
nr: #3 dodano: 2017-01-05 11:01
You can understand it by reading this article
Unary operator. Attempts to convert the operand to a number, if it is
nr: #4 dodano: 2017-01-05 11:01
+"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.