Thomas
Thomas

Reputation: 5089

Javascript testing whether or not a variable is set

Generally, I test whether or not a variable is set with something like this:

if (variable !== '') {

   do something...

}

I know there are other methods for testing variables like typeof but I don't see any advantage - is this an appropriate way to test whether or not a variable is set? Are there problems with it that I should be aware of ?

Upvotes: 7

Views: 24549

Answers (5)

Jordan Moncharmont
Jordan Moncharmont

Reputation: 169

Check out coffeescript's existential operator, by searching "The Existential Operator" on this page: http://coffeescript.org/

The functional problem with your approach is that is that you may inadvertently assign a blank string to variable at some point prior in your script and your logic block will now do the wrong thing.

From a stylistic standpoint your solution is less desirable because your intent to check the existence of the variable is not clear. Someone who was just reading through your code for this the first time might misunderstand what you wrote to mean "I'm expecting there to be a variable named variable set to the blank string" as opposed to "Do something if this variable does not exist."

Upvotes: 2

nnnnnn
nnnnnn

Reputation: 150040

If variable has been declared but might not have a value then your code:

if (variable !== '') {

tests if it is not the empty string. Is that what you want? An empty string might be a valid value. Better to test for undefined, or explicitly initialise it to a value that you can then treat as "invalid" (perhaps null, or whatever suits).

If variable has not been declared at all the above code would result in an error such that execution would stop at that point - you can't test the value of a variable that doesn't exist. So if, for example, you're trying to test a global variable that is created inside a function that may not have been called yet, or perhaps you're using several JS files and one needs to test a variable that may or may not have been created by one of the other files, then the only way to do it is with:

if (typeof variable != "undefined") {

Upvotes: 5

Eugen Rieck
Eugen Rieck

Reputation: 65284

This might be highly subjective, but my recommendation is to avoid code, that needs to check, whether a variable is set (a.o.t. has some value or type).

Consider this snipplet

var a=false;
if (some_condition) a="Blah";
if (typeof(a)=='string') ....
if (a===false) ...

this makes sure, a is always set, while keeping it easily differentiable from '', null or 0

Upvotes: 0

Nick
Nick

Reputation: 9154

Two reasons:

1) What if the variable is set by getting the contents of an empty input box?

if(someScenario){
    var variable = $('empty-box').val(); }

Perhaps this is only done in certain cases, like when someScenario is true. Later on, you want to check if that variable was set. Your means returns false rather than true. Point is, you can come up with scenarios where you get wrong answers.

There's just no reason not to do it the accepted way.

if(typeof variable !== 'undefined')

It's no slower, has no real flaws, and is only a few characters more.

2) And most importantly, using typeof makes it totally clear what you're asking. Readability is crucial, and if another programmer read the first code, they would think you were checking that it wasn't an empty string. The method using typeof makes it perfectly clear what your conditional is looking for, and reduces the odds of mistakes later on.

Upvotes: 23

jeff
jeff

Reputation: 8348

Since you're using strict equality testing, the following will all return true:

  • false
  • undefined
  • null
  • 0

The only time your check will return false is when you pass in an empty string.
Is that what you want?

Upvotes: 3

Related Questions