Two different results for "1" + "1" and "1" - - "1" in javascript

JavaScript enforcement, order priority and associativity can be confusing, but I use the link below to figure this out.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence

But I still don’t understand why "1"+"1"as a result "11"and "1"- - "1"as a result 2,

- -should be converted to +, and it should be handled as "1"+"1". What am I missing here?

You can check it out here:

console.log("1" + "1");
console.log("1"- - "1");
Run codeHide result
+4
source share
2 answers

- - -. , "1"- - "1" "1" - (-"1"), "1" - (-1), - , 1 - (-1), 2.

+7

"1"+"1" . + , string, -.

"1" - - "1" javascript - , . , NaN. , "1" - - "1" "1" - (- "1"). , , -, "1" ( ) (-"1") .

:

  • "foo" + 1 foo1, +

  • : "foo" - 1 NaN, . / *.

+4

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


All Articles