Suppose I have several environment variables, going by the hypothetical names OBJECT_A, OBJECT_B, OBJECT_C, etc.
I must implement a function doTheyExist() that must return a boolean.
I want to know what is the cleanest (as in, as little code as possible and as easy to read as possible) and the most efficient (as in using as few resources as possible) to implement such a function.
function doTheyExist() {
if (
process.env.OBJECT_A &&
process.env.OBJECT_B &&
process.env.OBJECT_C &&
...
) return true;
return false;
}
I initially thought I could just use a return (object_a && object_b && ...); would be the way to go, as it saves a line, but turns out it might return an undefined, rather than a false. I cannot have that. The return must be strictly true or false. Is there any cleaner way of doing this? And I know this is a minor thing and won't affect overall performance even if I were to check for thousands of variables, but is this the most efficient way of making checks like these?
[...].every(Boolean), maybe?Boolean(object_a && object_b &&...)or use ternaryobject_a && object_b &&... ? true : false!undefinedis true.!!undefinedis false.