JavaScript Without “if”

Tue, 01 Jul 2008

For some reason I have a question in my head: “Could I write code in JavaScript without ‘if’ statements at all?” Inspired by Chris Owen’s presentation on SmallTalk I created this implementation of SmallTalk-like pattern for working with boolean objects:

Boolean.prototype.ifTrue = function (f) {
    this && f();
    return this;
};
Boolean.prototype.ifFalse = function (f) {
    this || f();
    return this;
};

// so you can write

(4 < 5).ifTrue(function () {
    alert("It is true.");
}).ifFalse(function () {
    alert("It isn’t true.");
});

This example doesn’t have any practical usage, but from academic point of view this is an interesting example.