Ben Aston Ben Aston - 2 months ago 14
AngularJS Question

What is the benefit of angular.isdefined?

What is the benefit of

angular.isdefined
over and above
foo === undefined
?

I can't immediately think of a benefit.

Answer

Accessing a truly undefined variable in any way in Javascript, except tyepof throws an error. You can only use Angular.isDefined with properties. E.g, this would work fine:

angular.isDefined(window.obj);

Because obj is an undefined propery of window.

Examples of the expected behavior:

var foo;
var bar = 42;

typeof foo !== 'undefined'; // false
typeof bar !== 'undefined'; // true
typeof baz !== 'undefined'; // false

angular.isDefined(foo); // false
angular.isDefined(bar); // true
angular.isDefined(baz); // ReferenceError