Search code examples
javascriptcomparisonoperators

Any value that makes a JavaScript comparison always true?


Is there any JavaScript value that makes a comparison always true?

Example with lower than operator:

true < 10           true
false < 10          true
null < 10           true

Example with greater than operator:

true > 10           false
false > 10          false
null > 10           false

What I'm looking for:

alwaysTrue < 10     true
alwaysTrue > 10     true

I want to use this to make one part of an if statement return true by default and true or false when the first comparison value is changed.

This is probably not existent but I want to be completely sure.


Solution

  • You may want to consider leveraging "or" in your condition with another variable that can trump whether it returns true or not.

    returnTrue || testVariable < 10
    

    This will always return true when returnTrue is set to true, otherwise it will fall back on the comparison. If you are just looking for a change in a variable you can do so by storing the old value. If you know it will never be null you can check on this, otherwise you can use the the "or" with a flag similar to above.

    oldValue === null || currentValue === oldValue