Javascript typeof Operator vs Undefined

Introduction

Demonstrating how checking the typeof of a variable is more robust than checking equality to undefined


let myUndefinedVar;
if (myUndefinedlet === undefined) {
  console.log("1: myUndefinedlet is undefined.<br/>"); 
}
let undefined = "undefined";
if (myUndefinedlet === undefined) {
  console.log("2: myUndefinedlet is undefined.<br/>"); 
}
if (typeof myUndefinedlet == "undefined") {
  console.log("3: myUndefinedlet is undefined.<br/>"); 
}



PreviousNext

Related