Auto Convert to JavaScript

The following JavaScript expressions are pretty obvious.

var x = 10 + 10; 

The value of x is 20 .

 x = 10 + '10'; 

The value of x in this case is 1010 , because the + operator is overloaded. If any of the operands is of type string, string concatenation is performed, and if all operands are numbers, addition is performed.

 x = 10 - 10; x = 10 - '10'; 

In both cases, the value of x will be 0 , because the - operator - not overloaded in this way, and all operands are converted to numbers if they are not before the actual subtraction (you can clarify if I am mistaken in any case).


What happens in the following expression.

 x = '100' - -'150'; 

The value of x is 250 . Which also seems obvious, but this expression looks somewhat equivalent to the following expression.

 x = '100' +'150'; 

If that were the case, then the two lines would be merged and assigned 100150 to x . So why is the addition done in this case?


EDIT:

+'10' + 5 returns 15 and 'a' + + 'b' returns aNaN . Does anyone know why?

+8
javascript
source share
5 answers

In your case - - not evaluated first to become equivalent to + . -"150" evaluates to a number and becomes -150 .

Since you cannot subtract the string ( NaN ), JS then take "100" and make a number, and then run 100 - -150 , which is 250.

The key really is that you cannot subtract a type string, so it converts those strings to numbers.

+3
source share

The + and - operators respond differently to strings.

The + operator concatenates strings; however, the statement operator does not do the opposite (split lines).

So, if JavaScript sees '100' +'150' , it thinks, "Hey, I see lines with + ... I can concatenate them."

If JS sees '100' - -'150' , he thinks: "Hey, I see strings with ... - I cannot perform any string functions, so I will consider them as numbers ..."

+3
source share

Unary operator - always converts its operand to a number (ECMA-262 p. 11.4.6). So

 x = '100' - -'150'; 

equivalently

 x = '100' - -150; 

which further decreases by

 x = 100 - -150; 

because the binary operator also always converts its operands to a number (p. 11.6.2).

In contrast, the unary + operator converts its operands to strings if one of them is already a string (section 11.6.1).

You can find the full specification for ECMAscript (and therefore for the Javascript core) at http://www.ecma-international.org/publications/standards/Ecma-262.htm .

+2
source share

If JS sees the minus operator used in the string, it first tries to enter its number and then evaluates the expression, because the Minus operator is used only for arithmetic operations. Plus, the operator may mean first concatenation, and then addition.

In some other weakly typed languages, such as PHP, this ambiguity is eliminated by using two different operators for concatenation and addition.

However, the correct way to use arithmetic for strings is to enter them manually in numbers (using parseInt ).

+1
source share

Adding

This table shows the result of various conversions, where the variable s=string and n=number . You can also try your own values ​​using the provided code snippet.

This topic answered some questions that I had. And so I am posting the results of my tests to help others who come here to look for answers.

 ╔═════════════╦═══════════╦════════╗ β•‘ INPUT β•‘ VALUE β•‘ TYPEOF β•‘ ╠═════════════╬═══════════╬════════╣ β•‘ n β•‘ 11.5 β•‘ number β•‘ β•‘ s β•‘ -1.5 β•‘ string β•‘ β•‘ s - 0 β•‘ -1.5 β•‘ number β•‘ β•‘ n + s - 0 β•‘ NaN β•‘ error β•‘ β•‘ n + (s - 0) β•‘ 10 β•‘ number β•‘ β•‘ s + 0 β•‘ -1.50 β•‘ string β•‘ β•‘ n + s + 0 β•‘ 11.5-1.50 β•‘ string β•‘ β•‘ n + (s + 0) β•‘ 11.5-1.50 β•‘ string β•‘ β•‘ n + s β•‘ 11.5-1.5 β•‘ string β•‘ β•‘ s + n β•‘ -1.511.5 β•‘ string β•‘ β•‘ +s + n β•‘ 10 β•‘ number β•‘ β•‘ n + +s β•‘ 10 β•‘ number β•‘ β•‘ n++s β•‘ β•‘ error β•‘ β•‘ n+(+s) β•‘ 10 β•‘ number β•‘ β•‘ Number(s)+n β•‘ 10 β•‘ number β•‘ β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•©β•β•β•β•β•β•β•β•β•β•β•β•©β•β•β•β•β•β•β•β•β• 

 var n = 11.5, s = '-1.5'; add('n'); add('s'); add( - 0'); add('n + s - 0'); add('n + (s - 0)'); add( + 0'); add('n + s + 0'); add('n + (s + 0)'); add('n + s'); add( + n'); add('+s + n'); add('n + +s'); add('n++s'); add('n+(+s)'); add('Number(s) + n'); function add(eq) { var v, r, t; try { v = eval(eq); t = typeof v; } catch(e) { v = ''; t = 'error';} if (t=='number' && isNaN(v)) t = 'error'; r = window.stdout.insertRow(); r.className = t; r.insertCell().innerHTML = eq; r.insertCell().innerHTML = v; r.insertCell().innerHTML = t; } 
 table { border-collapse: collapse; font-family: sans-serif; } td { min-width: 5em; padding: 2px; border: 1px dimgray solid; text-align: right; } tr { background-color: lightgreen; } .string { background-color: lightyellow; } .error { background-color: pink; } 
 <table id="stdout"><caption>Type Conversion Results</caption></table> 
+1
source share

All Articles