⏱ 0:00est. 3 min
Chapter 11- Conceptual Aside (Javascript and 'undefined')
Notes
Example 1
var a ;
console.log(a); //undefined
----------------------------------------------------------------------------------
Example 2
console.log(a); //Error : a is not defined
----------------------------------------------------------------------------------
undefined and not defined are not the same thing in js. In js undefined is not just a word, It's actually a special value (undefined is a special keyword) that javascript have in it internally i.e the variable hasn't been set
Example 3 To prove above statement.
var a ;
console.log(a); //undefined
if(a === undefined){
console.log("a is undefined"); // this will work
}else{
console.log("a is defined");
}
------------------------------------------------------------------------------------
Example 4 Output :Uncaught reference error : a is not defined.
console.log(a);
if(a === undefined){
console.log("a is undefined");
}else{
console.log("a is defined");
}
It didn't find var a so it didn't set up memory space
Never do var a = undefined;
Never set a variable equal to undefined. It's perfect js but it's dangerous undefined means a programmer never set that value. It helps in debugging code. It would be difficult to identify that if you set it or js engine set it,if certain value is undefined