Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is this ==- javascript operator?

Tags:

javascript

I was stumbling around trying different conditions, when I discovered ==-, or ==+.

In a JS console, you can write:

var a = " ";

then the following is true

a == " ";

but this is false

a == "    ";

However, it will be true if you say:

a ==- "   ";

or

a ==+ "   ";

So what is this nifty ==- operator?

like image 759
user1674951 Avatar asked Sep 15 '12 22:09

user1674951


People also ask

What is == operator in JavaScript?

The equality operator ( == ) checks whether its two operands are equal, returning a Boolean result. Unlike the strict equality operator, it attempts to convert and compare operands that are of different types.

What is this == operator called?

Relational Operators == (Equal to)– This operator is used to check if both operands are equal. != (Not equal to)– Can check if both operands are not equal. >

What is == and === in JavaScript?

The main difference between the == and === operator in javascript is that the == operator does the type conversion of the operands before comparison, whereas the === operator compares the values as well as the data types of the operands.

What does '$' mean in JavaScript?

$ is simply a valid JavaScript identifier. JavaScript allows upper and lower letters, numbers, and $ and _ . The $ was intended to be used for machine-generated variables (such as $0001 ). Prototype, jQuery, and most javascript libraries use the $ as the primary base object (or function).


2 Answers

They're not distinct operators.

Writing:

a ==- " ";

gets parsed as:

(a) == (-" ");

The same goes for ==+.

The expression evaluates to true because of Javascript's weird type conversion rules. Something like the following occurs:

  1. the unary - (or +) operators converts its operand to a number. If it's a blank string, the result of this conversion is 0.
  2. a == (-" ") is then equivalent to " " == 0. If types compared with == are different, one (possibly both), get converted to get a common type. In this case, the " " on the left-hand side gets converted to 0 too.
  3. You end up comparing 0 to 0, which yields true.

(The above is a rough example of how Javascript might come to this result, the actual rules are buried in the ECMAScript specification. You can use the === operator instead to prevent the conversions and get false if the types of the compared objects are different.)

like image 135
millimoose Avatar answered Oct 13 '22 01:10

millimoose


It's simply a == followed by a - (or +).

(In the following I write "<four spaces>" to mean the string consisting of four spaces.)

That is, if you do " " ==- "<four spaces>", you compare " " to -"<four spaces>". -"<four spaces>" evaluates to 0, since applying the minus converts to integer. Thus, you actually do " " == 0, which is true, since it converts the " " to an integer for the comparison.

" " == "<four spaces>" is false, however, as you're comparing two different strings.

like image 44
Sebastian Paaske Tørholm Avatar answered Oct 12 '22 23:10

Sebastian Paaske Tørholm