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.

Problem courtesy of: Ben Aston


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


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
Solution courtesy of: Bharath Kumar Bachina


Here is the source:

function isDefined(value) {return typeof value !== 'undefined';}

Obviously the first reason is a lower verbosity, but it also future proofs angular, especially if the function is used internally.

Discussion courtesy of: Matt Way

I can only guess but I think my guess is a pretty good one.

These two expressions are functionally equivalent:

typeof foo !== 'undefined'


Benefits of the latter include:

1) It's arguably less of a mental strain to ask whether something is defined than to ask if something is not undefined.

2) angular.isDefined(foo) is arguably a lot less "noisy" than typeof foo !== 'undefined', and therefore it's quicker to grasp what's happening.

Note: These aren't my arguments for the superiority of angular.isDefined. What I'm trying to convey is my guess as to why the Angular team wanted to create angular.isDefined and why they thought it was better than the plain JavaScript alternative.

Discussion courtesy of: Jason Swett

Like Kamrul said angular does:

function isDefined(value){return typeof value !== 'undefined';}

Which means "the type of this var is undefined"... in you example you compare the content of the variable is equals to undefined and angular is checking the type of the variable.

In js the types are dynamic so until you don't assign a value the variable has no type... so isDefined will tell you both, if a variable declaration exist and if this variable has any content.

But, be careful because the variable could be null, in which case the type of the variable would be object.

You could try this code:

var a;
var b='asd';
var c=null;

console.log('a: '+typeof a);
console.log('b: '+typeof b);
console.log('c: '+typeof c);
console.log('d: '+typeof d);

And you will see the next in console:

a: undefined
b: string 
c: object 
d: undefined


a) the var exist but has no value so is undefined

b) the var exist and has value.. this value is a string so this is its type

c) the var exist but is null, the type can't be interfered so its type is object

d) the var has not been declared so... it's undefined

The main point is the diference between "a" and "d"... so try the next:

console.log('a is undefined? ' + a===undefined);
console.log('d is undefined? ' + d===undefined);

You will see the next in console:

Uncaught ReferenceError: d is not defined

Which... is a big problem because:

a) tells you that is not undefined when that's not true

d) raise an exception so... you code will fail


Use is defined when you want to check if a variable exists and has been initialized with a value, but be careful with null values because null is an object (so is a defined var).

If you want to validate that a variable exists and has any valid value (so is not null) you can simple do something like:

  console.log('myvar is defined and is not null');
    console.log('myvar is undefined or null');

Another good trick is to init to some value if the var is not defined with ||

myvar= myvar || 'some init value';

The above code takes the value of myvar if is defined and not null and if not init it with some value.

This is good in functions, for example:

function split(input, charToSplit){
  charToSplit= charToSplit || ' ';
  return input.split(charToSplit);

Then by default you can split with whitspaces: var input= 'asd asd'; var splited= split(input); // --> splited= ['asd','asd']

Or... with another char:

var input= 'asd|asd';
var splited= split(input,'|');
// --> splited= ['asd','asd']
Discussion courtesy of: Carlos Verdes

This recipe can be found in it's original form on Stack Over Flow.