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.


Solution 1:

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:

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

Solution 2:

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.

Solution 3:

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

So,

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:

false
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

Conclusion

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:

if (myvar) {
  console.log('myvar is defined and is not null');
} else {
    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.

As @TonyBrasunas put on his comment if myvar is evaluated to false, 'some init value' will be assigned. Take this into consideration before using this trick.

This is good in functions, for example:

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

Then by default you can split with whitespaces: 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']