Simplify nested ternary in Javascript

AngularDebutant :

I have the following code:

return this.condition1
        ? condition2
          ? true
          : false
        : false

Is there a way to write this without using true false?

Pointy :

Yes, you can simplify that; you don't need any ternary operators:

return this.condition1 && condition2;

Performing a test in order to choose either true or false is redundant. You can use !! to turn a "truthy" value into a boolean value, and if the test expressions already provide boolean values (such as > or < comparisons) then you've already got the values you need.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=220491&siteId=1