What is the == - javascript operator?

I stumbled over various conditions when I discovered ==-or ==+.

In the JS console, you can write:

var a = " ";

then the following is true:

a == " ";

but it is false

a == "    ";

However, it will be true if you say:

a ==- "   ";

or

a ==+ "   ";

So what is this great ==-operator?

+5
source share
3 answers

These are not different operators.

Record:

a ==- " ";

analyzed as:

(a) == (-" ");

The same goes for ==+.

The expression is evaluated as truedue to conversion rules like weird for Javascript. The following occurs:

  • unary operators -(or +) convert their operand to a number. If it is an empty string, the result of this conversion is 0.
  • a == (-" ") " " == 0. == , (, ), . " " 0.
  • 0 0, true.

( , Javascript , ECMAScript. ===, false, .)

+9

a ==, - ( +).

( "<four spaces>" , .)

, " " ==- "<four spaces>", " " -"<four spaces>". -"<four spaces>" 0, . , " " == 0, , " " .

" " == "<four spaces>" , , .

+3

. , - + , 0, false, == , ===, , :

console.log(a === ' '); // true
console.log(a === '   '); // false
console.log(a === -'   '); // false
console.log(a === +'   '); // false
+1
source

All Articles