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?
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.
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. >
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.
$ 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).
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:
- (or +) operators converts its operand to a number. If it's a blank string, the result of this conversion is 0.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.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.)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With