Skip to content

Instantly share code, notes, and snippets.

@andersr
Created October 7, 2016 12:58
Show Gist options
  • Save andersr/8693b5e393e0b59f8ec27576820a0aa0 to your computer and use it in GitHub Desktop.
Save andersr/8693b5e393e0b59f8ec27576820a0aa0 to your computer and use it in GitHub Desktop.
JS check if undeclared, undefined or null
//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