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;Written by Ben Howdle
Related protips
2 Responses
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
Best
#Javascript
Authors
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
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#