Last Updated: February 25, 2016
·
752
· 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 ·

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Related Tags
#javascript
Did you know?

JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.

Security insights from Cside