Variables

Test if a variable is defined

  • typeof is safer as it allows the identifier to never have been declared before:
// if (typeof neverDeclared === "undefined") // no errors
// if (neverDeclared === null) // throws ReferenceError: neverDeclared is not defined

if (typeof variable_name !== 'undefined') {
    console.log(variable_name + ' exists!');
}