Why should I use underscore's isUndefined(x) over x === undefined?
javascript, underscore.js
Solution
The name `undefined` can be shadowed. That is, somebody could do this
var undefined = 5;
and break the code that uses `x === undefined` (see note at bottom). To get around this safely, you can use
typeof x === 'undefined'
or
x === void 0
which is exactly what the underscore function does.
Note: Since ECMAScript 5, `undefined` is read-only. In older browser, the global `undefined` can be redefined. Even in newer browsers, `undefined` can be shadowed by a local variable:
function f() {
var undefined = 5;
return undefined;
}
f() // returns 5
Problem
Is there any benefit in using isUndefined? Is it worth an extra function call? It's not any more readable.