At many places, I've seen developers doing value == var
comparisons, like this:
if ('https' === location.protocol) {
port = 8443;
protocol = 'wss://';
isSecure = true;
}
I know that a == b
is same as b == a
, so why do people use value == var
instead of var == value
?
Is there a standard for this? And if yes, which is the standard way?
What you are seeing is yoda condition.
Yoda conditions describe the same expression, but reversed:
if ( 42 == $value ) { /* ... */ }
// Reads like: "If 42 equals the value..."
The advantage is
Placing the constant value in the expression does not change the behavior of the program (unless the values evaluate to false—see below). In programming languages that use a single equals sign (=) for assignment and not for comparison, a possible mistake is to assign a value unintentionally instead of writing a conditional statement.
Note that it is clearly lack of readability. I personally don't prefer this way.
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