a1exlism
1/5/2018 - 7:13 AM

variable_check

function foo(bar) {
  bar = bar || 'test';  //  bad way
}

/*
 * in boolean mode, you should be careful with it beacause `||` also is an Or operation!!
 *
 * the better way : downbelow 
 * from: https://stackoverflow.com/questions/858181/how-to-check-a-not-defined-variable-in-javascript
 * In ES6, thie prob will be much easier
 *
*/


//  check if a variable is declared and is not undefined:
if (typeof yourvar !== 'undefined') // Any scope

//  variable exists: check whether there's any value stored in it
if (yourvar !== undefined)

//  If you want to know if a member exists independent but don't care what its value is:
if ('membername' in object)              // With inheritance
if (object.hasOwnProperty('membername')) // Without inheritance