3 ms·
This is a matter of taste, of course, as we don't have a good study of programming code comprehension yet, but I think the coolest part of JS are cond statement
by andreyf 18y ago
This is a matter of taste, of course, as we don't have a good study of programming code comprehension yet, but I think the coolest part of JS are cond statements using the trinary operator:
var foo = predicate ? "one" :
predicate2 ? "two" :
"default";
And short-circuit and/or:
var foo = uncheckedFoo || "default";
var wiz = (bar && bar(foo)) || "default";
Truthy values get in the way sometimes (especially 0), but it sure as hell beats a ton of if...else crud.
Worst part of JS, while we're at it:
true == "0" == 0 == false
- andreyf 18y agoHere's another neat example of short-circuit or: function getFoo(f) { if (f) { if (foos[f]) { return foos[f]; } else { return f; } } else { return "error"; } } Is (almost) the same as... function getFoo(f) { return foos[f] || f || "error"; } The difference in behavior being if foos[undefined] is set...
- boucher 18y agotrue == "0" == 0 == false This is untrue. > true == "0" false > "0" == 0 true > 0 == false true > true == "0" == 0 == false false
- DougBTX 18y agoI think he meant something like this: true == !0 == "0" == false Which still isn't that interesting, since it is much the same as: true == (false == false) More interesting, something like: > ! 0 == "0" false > 0 == ! "0" true Since "!" acts as if false === 0.