-1

I have an array filled with the names of variables like this:

var myVariables = [variable1,variable2,variable3,variable4];

Is there a simple way besides and each to test if all of these variables have been assigned a value (elsewhere in my code)?

jonmrich
  • 4,233
  • 5
  • 42
  • 94
  • Do you mean to check if none of them is `undefined`? – kind user Aug 03 '17 at 20:21
  • Currently you have an array with the **values** of these variables, not their names. This contradicts your description. What do you really have now? – Felix Kling Aug 03 '17 at 20:21
  • @Kinduser Right – jonmrich Aug 03 '17 at 20:22
  • @FelixKling The variables are defined elsewhere (or not at all). Basically, assume I have a list of variable names and I want to check them to see if they are undefined. – jonmrich Aug 03 '17 at 20:23
  • 1
    `[variable1]` puts the **value** of `variable1` into the array, **not** its *name*. So if this is the code you actually have, then you simply have an array of values, not an array of variable names. Hence I'm asking you to clarify what you actually have. – Felix Kling Aug 03 '17 at 20:24
  • 1
    *"The variables are defined elsewhere (or not at all)"* If a variable is not declared, then trying to reference it will throw a `ReferenceError`. I.e. `[variable1,variable2,variable3,variable4]` will throw an error if one of these variables is not declared. You'd have to test its existence *before* referencing it. – Felix Kling Aug 03 '17 at 20:27
  • on a side note, how would I define an array of variable names? – Huangism Aug 03 '17 at 20:32

4 Answers4

1

I would suggest using Array.some, with that approach there is a chance you won't have to iterate the entire array:

const hasEmpty = myVariables.some(v => typeof v === 'undefined');
Rob M.
  • 35,491
  • 6
  • 51
  • 50
  • How can I use the output of this? Can I use `hasEmpty` in, say, an `if/then`? And is the value of hasEmpthy `true` or `false` only? – jonmrich Aug 03 '17 at 20:30
  • Correct, `hasEmpty` is a boolean value – Rob M. Aug 03 '17 at 20:30
  • And what's its scope? Can I use it anywhere in my script? – jonmrich Aug 03 '17 at 20:31
  • That depends on a lot of external factors - 'use strict', the context it is running (in a module, in a global scope script), how you define it (e.g. `window.hasEmpty...` vs. `let hasEmpty...`.). You can certainly make it available to any part of your script, or calculate/set it when and where you need it. – Rob M. Aug 03 '17 at 20:32
  • Short answer but it addresses well the matter. – davidxxx Aug 03 '17 at 21:17
1

You could use the Array.prototype.some() method :

The some() method tests whether some element in the array passes the test implemented by the provided function.

It could be more efficient than forEach method as it stops iterating (short circuit in a some way) as soon a element matches the condition.

For example to check that all elements are > 0, use some() with the reverse condition, that is : <=0.

var isFailed = [0, 1, 2, 3, 4].some(x => x <= 0); 

For example, here, as soon the first iteration, some() exits and return false.

davidxxx
  • 125,838
  • 23
  • 214
  • 215
0
return myVariables.indexOf(undefined) === -1;
Ulysse BN
  • 10,116
  • 7
  • 54
  • 82
Our_Benefactors
  • 3,220
  • 3
  • 21
  • 27
0

If one of your variable is not defined, this will throw a ReferenceError such as mentionned in top comments by Felix Kling. Otherwise if it has the value undefined then you can check if your array contains undefined values.

don't throw Reference Error

If you execute the code below you will get a ReferenceError since variable1 has never been defined.

const myArray = [variable1]

But this next code will just create an array with an undefined value in it, since the variable is declared:

let variable1
const myArray = [variable1]

check if undefined is included

With ES 2016 includes():

const isAllDefined = !myVariables.includes(undefined)
// use this boolean where you need it
Ulysse BN
  • 10,116
  • 7
  • 54
  • 82
  • is this faster than using indexOf? It's certainly less readable. EDIT: I suppose using includes is readable enough. – Our_Benefactors Aug 03 '17 at 20:25
  • I didn't think about indexOf, and don't know about benchmark. For the readability, check the includes version :) I've even edited with only includes, best practice IMHO. – Ulysse BN Aug 03 '17 at 20:26
  • Is `isAllDefined` a boolean? What's its scope? Can I use it anywhere in my script? – jonmrich Aug 03 '17 at 20:31
  • Yes it is a boolean. For the scope this is a totally different question, https://stackoverflow.com/a/500459/6320039. – Ulysse BN Aug 03 '17 at 20:34