0

In many place, I've seen that there is used the expression if (value === expression) rather than if (expression === value).

For example, In php we use-

if (false === strpos('abc', 'a'))

And also I see after minifying JavaScript, the minified file also generated like this.

So my question is, what is the benefit of value === expression over expression === value?

Note: This question may be redundant but I may not get the proper keywords for searching. If it is duplicated then I am ready to close the question.

Sajeeb Ahamed
  • 6,070
  • 2
  • 21
  • 30
  • 6
    You may be looking for [Yoda conditions](https://en.wikipedia.org/wiki/Yoda_conditions). It makes no difference. – elclanrs Oct 09 '20 at 07:01
  • 4
    The only disadvantage of `value === expression` is that it upsets me on a deep level when I have to read it :) –  Oct 09 '20 at 07:02
  • @ChrisG You misspelled "advantage" >:) – Clément Baconnier Oct 09 '20 at 07:05
  • 1
    @elclanrs maybe I am looking for this. Could you please write a clear answer for future reference with pros, and cons (if any). – Sajeeb Ahamed Oct 09 '20 at 07:08
  • 2
    This (AFAIK) was a result of mistakes writing `$a = false` which would assign a value, `false = $a` on the other hand is an error. – Nigel Ren Oct 09 '20 at 07:14
  • 1
    There's also an [eslint yoda rule](https://eslint.org/docs/rules/yoda) for this. Pro is that you cannot mistakenly assign (`=`) in the comparison, con is the worse readability – A_A Oct 09 '20 at 07:16

1 Answers1

0

I don't think there is a difference because the idea of equality operator is that to check if both sides are have the same value, in === also same type.

27px
  • 430
  • 5
  • 16