JavaScript Unexpected results in JavaScript unexpected results in javascript

Using the node.js console, I get the following unexpected results:

> 2 + "3"
"23"

> 2 * "3"
6

Why does the first example favor string concatenation and integer multiplication in the second example? I would not expect concatenation between different types, but rather an error to be thrown. If this is the behavior in JS, how can I predict the type of the final result?

Answer:1

According to ECMAScript 2015 Language Specification (Addition operator, Multiplicative operators), the evaluation of additive expression is (lprim is the left primitive (lval converted to primitive), rprim is the right one):

 ...
 If Type(lprim) is String or Type(rprim) is String, then
     Let lstr be ToString(lprim).
     ReturnIfAbrupt(lstr).
     Let rstr be ToString(rprim).
     ReturnIfAbrupt(rstr).
     Return the String that is the result of concatenating lstr and rstr.
 Let lnum be ToNumber(lprim).
 ReturnIfAbrupt(lnum).
 Let rnum be ToNumber(rprim).
 ReturnIfAbrupt(rnum).
 Return the result of applying the addition operation to lnum and rnum.

The evaluation of multiplicative expression is:

...
Let lnum be ToNumber(leftValue).
ReturnIfAbrupt(lnum).
Let rnum be ToNumber(rightValue).
ReturnIfAbrupt(rnum).
Return the result of applying the MultiplicativeOperator

You can see that if it is an additive expression, it is first to check whether there is a String. If there is a String, string concatenation is executed. Otherwise, the values are converted to Number and an addition operation is executed.

If it is a multiplicative expression, it would always convert values to Number and deliver a multiplicative operation.

Answer:2

I know this has been discussed in other threads, but I've very new to coding websites in general, and when I try to use the solutions others have come up with, they just don't seem to work. And I ...

I know this has been discussed in other threads, but I've very new to coding websites in general, and when I try to use the solutions others have come up with, they just don't seem to work. And I ...

  1. twitter bootstrap close modal

I want users to be able to take a picture programmatically of a design they have made on my site . The design would be in a div that is a panorama that extends off both left/right edge of the screen. ...

I want users to be able to take a picture programmatically of a design they have made on my site . The design would be in a div that is a panorama that extends off both left/right edge of the screen. ...

  1. screenshot is possible with htmlunitdriver
  2. screenshot if possible
  3. screenshot not possible

The new JS enabled page of http://mail.google.com is making trouble to get ID of password input field. However, I've navigated from email ID page with its ID - identifierID . Help me to get ID of ...

The new JS enabled page of http://mail.google.com is making trouble to get ID of password input field. However, I've navigated from email ID page with its ID - identifierID . Help me to get ID of ...

  1. password input field html
  2. password input field in react native
  3. password input field unity
  4. password input field in angular material
  5. password input field
  6. password input field css
  7. password input field django
  8. password input field validation
  9. password input field type html
  10. password input field type
  11. show password input field bootstrap
  12. show password input field
  13. angular password input field
  14. qt password input field
  15. show password input field jquery
  16. php password input field
  17. bootstrap password input field
  18. react password input field
  19. yii2 password input field
  20. hide password input field

The window.screenX and window.screenY properties only give the top left corner of the outer window, outside the content area (above the address bar and tabs). How can I determine the position of the ...

The window.screenX and window.screenY properties only give the top left corner of the outer window, outside the content area (above the address bar and tabs). How can I determine the position of the ...