MENU
'undefined' And 'null'
Out of the six primitive data types, two data types, 'undefined' and 'null', can only have one value, which is 'undefined' and 'null' themselves respectively. 'undefined' is a result of attempts to access variables which have not been initialized with a value. 'null' is a default placeholder for an object.
'undefined' and 'null' both evaluate to false in a Boolean expression, which explains why (a==b) is true. NaN (Not a Number) actually has the Number type.
<!DOCTYPE html>
<html>
<body>
<script>
var a;
console.log(typeof a);
console.log(typeof b);
var b = document.getElementById("x");
console.log(b);
console.log(a == b);
console.log(a === b);
var c = null;
var d = undefined;
console.log(c + d);
console.log(typeof (c + d));
var e = new Object();
console.log(e);
console.log(e.a);
if (typeof yourvar == 'undefined') console.log("variable does not exist");
</script>
</body>
</html>undefined
undefined
null
true
false
NaN
number
Object {}
undefined
variable does not exist
Some declarations are hoisted in JavaScript. This means that a variable can be used above its declaration within the scope. <br>Here v1 is initialized with 'undefined' initially. v2 and v3 stay inaccessible until they are declared. The period between entering scope and being declared is called the temporal dead zone.
<!DOCTYPE html>
<html>
<body>
<script>
console.log(typeof v1);
// console.log(typeof v2);
// console.log(typeof v3);
var v1 = 10;
let v2 = 20;
const v3 = 30;
</script>
</body>
</html>undefined
ReferenceError
ReferenceError