Skip to content Skip to sidebar Skip to footer

Are These Js Conditional Statements Functionally Equivalent?

Regarding conditional if/else statements, are the following examples functionally equivalent? function isEntering() { if (this.stage === 'entering') { return true;

Solution 1:

They are not all equivalent. The first two are equivalent, but:

function isEntering() {
    if (this.stage === 'entering') {
        returntrue;
    } 
}

Would return undefined if this.stage !== 'entering'.

Also:

isEntering = (this.stage === 'entering') ? true : false;

Is not defining a function as the other examples.

As mentioned you can add:

isEntering = () => this.stage === 'entering';

If you don't need a function you can use:

isEntering = this.stage === 'entering'

Post a Comment for "Are These Js Conditional Statements Functionally Equivalent?"