What is the benefit of angular.isdefined?

JavascriptAngularjs

Javascript Problem Overview


What is the benefit of angular.isdefined over and above foo === undefined?

I can't immediately think of a benefit.

Javascript Solutions


Solution 1 - Javascript

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 - Javascript

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 - Javascript

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']

Solution 4 - Javascript

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

These two expressions are functionally equivalent:

typeof foo !== 'undefined'

angular.isDefined(foo)

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBen AstonView Question on Stackoverflow
Solution 1 - JavascriptSanthi BharathView Answer on Stackoverflow
Solution 2 - JavascriptMatt WayView Answer on Stackoverflow
Solution 3 - JavascriptCarlos VerdesView Answer on Stackoverflow
Solution 4 - JavascriptJason SwettView Answer on Stackoverflow