News for hackers. Stuff that matters.
Re:Don't use || to set default values in JavaScript by ricklancee on Friday July 10, 2015 @08:17AM (#9863460)
Shouldn't the undefined check be like this?

    if (typeof fruit === "undefined") {
       fruit = "strawberry";
    }
Unless your doing something like:

    (function(undefined) {

    })();
Re:Don't use || to set default values in JavaScript by masklinn on Friday July 10, 2015 @08:27AM (#9863499)
`typeof` is useful when the binding may or may not exist at all, this is almost exclusively an issue with global variables which may not be there (toplevel APIs or libraries missing).

If the binding exists at all, you can just check for its value. And parameters always create a binding pretty much by definition.

So no, there is no reason to use `typeof` to check whether a parameter is `undefined`.

If you fear that somebody rebound the global `undefined` for some insane reason, you can use `void 0` instead.

Re:Don't use || to set default values in JavaScript by kevincennis on Friday July 10, 2015 @08:27AM (#9863497)
In ES5+, the global `undefined` is immutable, so it should be pretty safe unless someone declared a local `undefined` var in an outer scope.
Re:Don't use || to set default values in JavaScript by arnehormann on Friday July 10, 2015 @08:49AM (#9863617)
I use "var undef;" at the top of a function and never set it to a value. Comparison with === is the same as with undefined. And it's nice for minification.