JavaScript undefined

javascript 'undefined'

In javascript we might have seen the keyword undefined printing on console many times, but what this undefined is ? Who set it up as undefined and how we can make use of it ?
Let's make changes in our test.js javascript block and check in the console.
var a; console.log(a);
In the console the output would be
undefined

Now if we try console.log without declaring the variable we get execption at the console. console.log(a);
Uncaught ReferenceError: a is not defined
So how this a is not defined is different than undefined. Is it not defined not same as undefined ?
The short the answer is No; in javascript undefined and not defined as part of exception; it is not same.
In javascript during execution context creation (before execution) javascript engine starts allocating memory space for the variables. If the variable is not defined but declared, it allocates the keyword undefined.
So during execution if the variable still not set up with any value it shows undefined. var a; /* why using '==='' but not '=='' we will discuss later */ if(a === undefined){ console.log('a is undefined !'); } else{ console.log('a is defined'); }
 a is undefined ! 
However if you remove the declaration part var a; altogether, during execution context creation javascript engine doesn't allocate any memory space for the variable and thus during execution; engine doesn't recognizes the variable; so throws out exception and it exits out.
Uncaught ReferenceError: a is not defined
Back Next