0

Hey guys i have an some jQuery-JavaScript code and its made by some undefined variable.

I trying to skip (undefined) error by doing this code :

if(typeof undefined_var !== "undefined"){
    /*My code is here*/
}else{
    /*create variable*/
    /*My code is here*/
}

But the problem is i have a lot of variable and i have to use a big code like this :

if(typeof undefined_var1 !== "undefined" && typeof undefined_var2 !== "undefined" && typeof undefined_var3 !== "undefined" /* && more */ ){

And its not optimized i looking for something better than it like this :

if(undefined_var1 && undefined_var2 && undefined_var3)

Is there anyway?

  • Why can't you use `undefined_var1 !== undefined`? – Edric May 19 '20 at 13:39
  • @Edric its will be big for my project i mean its will be heavy code and its not good for me. –  May 19 '20 at 13:40
  • Does this answer your question? [How best to determine if an argument is not sent to the JavaScript function](https://stackoverflow.com/questions/411352/how-best-to-determine-if-an-argument-is-not-sent-to-the-javascript-function) – freedomn-m May 19 '20 at 13:54

2 Answers2

0

At whatever point that the variables are defined, put them onto a single object instead, then all you have to do is check if the object exists yet:

if (!window.myObj) {
  // Define properties:
  window.myObj = {
    prop1: 'val1',
    prop2: 'val2',
    // ...
  };
}
// proceed to use `window.myObj.prop1`, etc
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
0

You can make an array containing all those variables and then make a function which takes that array as an argument. Then inside the function loop through the array using the conditional (if statement) to determine if any are false. For example arr.reduce((bln, myVar) => typeof myVar === 'undefined' && bln, true). Call the function and it will return true or false depending on if any were undefined.

var _0;
var _1;
var _2 = 'not undefined';
var _3 = 'again not undefined';

const test0 = [_0, _1]; //should return true (contains undefined)
const test1 = [_2, _3]; //should return false (doesn't contain undefined)
const test2 = [_0, _1, _2, _3]; //should return true (contains undefined)

function containsUndefined(arr){
  //loop array to determine if any are undefined
  return arr.reduce((bln, myVar) => typeof myVar == 'undefined' && bln, true);
}

console.log('test0', containsUndefined(test0));
console.log('test1', containsUndefined(test1));
console.log('test2', containsUndefined(test2));
George
  • 2,330
  • 3
  • 15
  • 36
  • Isn't the "make an array" already done for you in the `arguments` objects in a function? https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments – freedomn-m May 19 '20 at 13:51
  • In that case, that will be an array inside an array. arguments[0] would contain an array. Additionally, as in that mdn article, it suggests we use '...' rest instead of arguments, now we have ES6. And that way will make the code less readable since you are defining test0, test1, test2 inline of the console.logs – George May 19 '20 at 13:51