-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?

3
  • 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: stackoverflow.com/questions/24502643/… Commented Aug 15, 2018 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. Commented Aug 15, 2018 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. Commented Aug 15, 2018 at 19:01

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

varToCheck !== 'undefined' <- this is comparing against the literal string 'undefined' and won't work, right? You need to remove the quotes.
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:

1 Comment

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!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.