7 ms·
Shouldn't the undefined check be like this? if (typeof fruit === "undefined") { fruit = "strawberry"; } Unless your doing something like:
by ricklancee 11y ago
Shouldn't the undefined check be like this?
if (typeof fruit === "undefined") {
fruit = "strawberry";
}
Unless your doing something like:
(function(undefined) {
})();
- kevincennis 11y agoIn ES5+, the global `undefined` is immutable, so it should be pretty safe unless someone declared a local `undefined` var in an outer scope.
- arnehormann 11y agoI 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.
- masklinn 11y ago`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.