Created
October 7, 2016 12:58
-
-
Save andersr/8693b5e393e0b59f8ec27576820a0aa0 to your computer and use it in GitHub Desktop.
JS check if undeclared, undefined or null
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//check if undeclared | |
try{ | |
undeclaredVar | |
} | |
catch(e) { | |
if(e.name === 'ReferenceError') { | |
console.log('var is undeclared') | |
} | |
} | |
// check if undefined | |
var undefinedVar | |
if (typeof undefinedVar === 'undefined') { | |
console.log('var is undefined') | |
} | |
// check if null | |
var nullVar = null | |
if (nullVar === null) { | |
console.log('var is null') | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment