-1

I'm looking for a way to do the following:

var test = 'I exist!';
var testNull = null;
var testUndefined;

function checkVar(varToCheck) {
  return typeof varToCheck != 'undefined' && varToCheck != null;
}

console.log(checkVar(test)); // Logs true
console.log(checkVar(testNull)) // Logs false
console.log(checkVar(testUndefined)) // Logs false
console.log(checkVar(undefinedVar)) // Logs false

When it tries to execute the last line, instead of false, this throws an error: Uncaught ReferenceError: undefinedVar is not defined.

I know it can be done with:

if (typeof varToCheck != 'undefined' && varToCheck != null) {
    alert('something happens!')
}

but it's becoming annoyingly repetitive to use long conditions in my project, once I have a lot of variables to check. Any ideas?

Eric Gruby
  • 389
  • 3
  • 11
  • 3
    There's nothing wrong with your function. `undefined` and `null` are values that can be assigned to variables. If you don't declare a variable, as in the case of `undefinedVar` then it is not defined, which is distinctly different from `undefined` and will always throw an error. Better explanation here: https://stackoverflow.com/questions/24502643/javascript-variable-undefined-vs-not-defined#24502710 – Robbie Milejczak Aug 15 '18 at 18:58
  • 1
    It's a sign of underlying problems with your design if you don't know if a variable is in scope at a given location. You should not have to test for this programatically except in a very narrow set of cases. – user229044 Aug 15 '18 at 18:59
  • 1
    I agree with @meagar here. It's hard to see when you need to test if a variable is declared. If there's uncertainty for some reason, you can wrap it in a `try/catch` block to catch the reference error. – Mark Aug 15 '18 at 19:01

2 Answers2

1

typeof is unnecesary. also, !== will check specifically for the type and not just falsy/truthy values.

function checkVar(varToCheck) {
  return varToCheck !== undefined && varToCheck !== null;
}

undefinedVar is not a variable with an undefined value, it was never declared to begin with, and will throw an error if you try reference it. However, object properties that weren't declared will not throw an error if you try reference them, and will compute to undefined.

StreamedLine
  • 141
  • 5
  • 3
    `varToCheck !== 'undefined'` <- this is comparing against the literal string `'undefined'` and won't work, right? You need to remove the quotes. – Ollin Boer Bohan Aug 15 '18 at 19:02
0

What I was looking for was a secure way to access values of nested objects.

I ended up using a tiny lib called Typy, which provided exactly the functionality I needed.

I will leave Typy and some other alternatives I've found:

Eric Gruby
  • 389
  • 3
  • 11
  • 1
    This probably should be better as an update to your previous answer, we are free to edit our posts any time that's necessary. Just be wary that editing your question doesn't invalidates any existing answer. Also, try to focus on the problem/solution at hand, anything unrelated is considered noise (like "Sorry for...", or "I hope this can help", or "If this sounds silly"). Cheers! – brasofilo Oct 26 '18 at 01:41