Strange behavior when using ++ in JavaScript

Everyone knows about the basic concatenation of two strings in JavaScript:

> "Hello " + "World!" 'Hello World!' 

But what happens if we use + + instead of + ? I just came across the following weird behavior:

 > "Hello " + + "World!" 'Hello NaN' > "Hello " + + "" 'Hello 0' 

From the above examples, it can be seen that the second line is converted to a number. Thus, by passing an object having the valueOf property as a function, the value returned by this function will be converted.

 > "Hello " + + ({valueOf: function () {return 1; }}) 'Hello 1' 

As expected, it shows "Hello 1" .


Why is the second line converted to Number ? Why not throw a syntax error or so?

+5
source share
3 answers

The second + is the unary plus operator, the purpose of which is to convert its operand to a number.

The unary plus operator precedes its operand and evaluates its operand, but tries to convert it to a number if it is not already. Although unary negation (-) can also convert non-numbers, unary plus is the fastest and preferred way to convert something to a number, since it does not perform any other operations on the number. It can convert string representations of integers and floats, as well as non-string values ​​of true, false and null. Integers in both decimal and hexadecimal ("0x" -representable) formats are supported. Negative numbers are supported (although not for hex). If he cannot analyze a specific value, it will be evaluated to NaN.

 "Hello " + + "World!" 

can only be processed as

 "Hello " + (+ "World!") 

which the

 "Hello " + NaN 

hence your result.

This unary operator is very useful in JavaScript and is one of the most common ways to convert from a number, which can be a number or a string for a number. It also has the advantage over parseFloat that it is not ridiculously tolerant ( parseFloat("7up") will return 7 ), which makes it an easy way to see if a string is a number representation (just a test s==+s ).

+3
source

Great question. This is due to the fact that JavaScript has a unary + operator (similar to unary - as in, ex, -1 , which is parsed by - (1) ): https://developer.mozilla.org/en-US/docs/ Web / JavaScript / Reference / Operators / Arithmetic_Operators # Unary_plus _ (. 2B)

Thus, "foo" + + "bar" parsed for "foo" + (+ "bar") (or if you prefer the lisp parsing tree: (+ "foo" (+ "bar") ).

Unary + converts its operand to a number or NaN if the operand is not a reasonable number:

 > +"42" 42 > +"foo" NaN 

Finally, adding a number (including NaN ) to a string results in string concatenation.

Thus, "foo" + +"bar" can be expanded:

 "foo " + +"bar" "foo " + NaN "foo NaN" 
+2
source

Here + is the unary plus operator , which tries to convert the operator to a number and works as follows

 +"" //return 0 +"Some" //returns NaN 

So here

 "Hello " + + "World!" 

will be

 "Hello " + (+ "World!") "Hello " + NaN "Hello Nan" 
+1
source

Source: https://habr.com/ru/post/1201261/


All Articles