Position of object property in an equation

Many, many moons ago when I was avidly consuming books and other js resources I believe I was warned that when comparing an object property with a variable that that it was wise to put the object property on the lefthand side of the equation.
Is this still true for today and why, or was it just a furphy?
Thanks in advance.

I see no real advantage in this recommendation. I do it mostly automatic because I find it easier to read but that only a personal feeling

I think that was someone trying to come up with a “best practice” pattern. But it hold no value in terms of performance.

I could see it being placed on either side. It would depend on which one was the “standard”. I personally would put the expected value on the right, and the one being checked on the left.

i.e.

$x = "value";
if (object.property == $x) { 
   // do something
}

for ($y in $ylist) {
  if ($y == object.property) {
     // do something
  }
}

only time i could see it being an issue is if you goof up a close parenthesis location and accidentally try to reference a property of the result of an operation:
"This is a string and the value is"+ (x.prop + y)+", so there"

"This is a string and the value is"+ (y+x).prop+", so there"

or perhaps it’s just for readability; is x + y.value meant to be (x+y).value or (x+y.value)?
(Especially since Javascript is a multiline-acceptor, so you could also have written this as

x+y
.value;

and confused a whole lot of people.)

There was an attempt long ago to help protect people from accidentally using = for comparisons.

  if ($y = object.property) {
     // do something
  }

That would help to save the property from being accidentally assigned.

I see little to no value in that kind of behaviour, as has already been mentioned by others here.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.