Occurrences of undefined and null

The following examples defined the Occurrences of undefined and null.

Occurrences of undefined :

If the variables are not initialized, then it is undefined.
var foo;
foo
undefined

Missing parameters are undefined.

function f(x) { alert(9); }
f()
undefined

If a property doesn't exist, then it returns undefined.

var a = {};
a.foo;
undefined

If a function doesn't has anything to return explicitly, then it returns undefined

function a() { }
a()
undefined

function b(){ return; }
b()
undefined

Occurrences of null :

1. null is the last element in the prototype chain.
      Object.getPrototypeOf(Object.prototype);
      null
2. null is returned even if the regular expression doesn't matches for the given regular expression in the string.

>/x/.exec('aaa')
null

To check whether the result is null or undefined...

if(a == null)....

if(a == undefined)...

Few Examples :

Number(null);
0

5 + null
5

Number(undefined)
undefined

5 + undefined
undefined

if( x == void 0 ) // Here, void 0 means, which is always undefined i.e, it is equivalent to if(x == undefined)




No comments:

Post a Comment

Overview of AngularJS

AngularJS is an open source web application framework. It is now maintained by Google . The most of the code can be eliminated by using th...