Can I safely pass an undefined object property to a switch in JavaScript?

Last modified: 
Wednesday, August 16th, 2017
Topics: 
Javascript

Q. Can I safely pass an undefined object property to a switch in JavaScript?

A. Yes, I can!

let myObj = {};
let testIt = myObj => {
  switch(myObj.undef) {
    case 'Some Value':
      return 'undef is defined';
      break;
    default:
      return 'undef is undefined but there is no error';
      break;
  };  
};
console.log(testIt(myObj)); // => "undef is undefined but there is no error"

JS Bin on jsbin.com


The operator of this site makes no claims, promises, or guarantees of the accuracy, completeness, originality, uniqueness, or even general adequacy of the contents herein and expressly disclaims liability for errors and omissions in the contents of this website.