Last Updated: February 25, 2016
·
558
· benhowdle89

Simpler JS boolean based conditions

Nothing new, just the path to nicer code that I always seem to take:

var result;
var big = 4;
var small = 3;

// first attempt
if(big > small){
    result = true;
} else {
    result = false;
}

// better
result = (big > small) ? true : false;

// awww yeah, nice
result = big > small;

2 Responses
Add your response

best:

result = big > small;
over 1 year ago ·

You forgot the intermediary result = (big > small) || false; // ;)

over 1 year ago ·