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.
RESETRUNFULL
RESETRUNFULL
<!DOCTYPE html><html><body><script>
<!DOCTYPE html><html><body><script>
var a;
console.log(typeof a); // undefined
console.log(typeof b); // undefined
var b = document.getElementById("x");
console.log(b); // null
console.log(a==b); // true
console.log(a===b); // false
var c = null;
var d = undefined;
console.log(c+d); // NaN
console.log(typeof (c+d)); // number
var e = new Object();
console.log(e); // Object {}
console.log(e.a); // undefined
if (typeof yourvar == 'undefined') console.log("variable does not exist");</script></body></html>
</script></body><html>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.
RESETRUNFULL
RESETRUNFULL
<!DOCTYPE html><html><body><script>
<!DOCTYPE html><html><body><script>
console.log(typeof v1); // undefined //console.log(typeof v2); // ReferenceError //console.log(typeof v3); // ReferenceError
var v1=10;
let v2=20;
const v3=30;</script></body></html>
</script></body><html>