Better way to check if Javascript Date object exists
I can think of the following improvements:


Hide t. If you're running other JS modules, you can't really be sure that no-one else writes to the same variable.
If you want to be sure that your code doesn't mix up an uninitialized timer with a "cleared" timer, you might want to pre-initialize the variable to null instead.
Use === when comparing null, undefined, etc. In JavaScript, null == undefined, but only null === null.


You might want to do something like this:

var timer = (function () {
    var t = null,

    start = function () {
        if (t === null) {
            t = new Date();
        }
    },

    stop = function () {
        var time = new Date() - t;
        t = null;
        return time;
    };

    return {
        start: start,
        stop: stop
    };
})();

timer.start();

var timePassed = timer.stop();