3

I was warned that this post could be too subjective, but I've only been programming for a few weeks, so I'd like to know.

So far I've been using try/catch statements in my JS to keep from throwing errors in case a variable isn't defined when a function is run, but is that the only efficient way to do so?

Brandon Lebedev
  • 2,717
  • 6
  • 24
  • 35
  • 1
    It's better to figure out why the errors are occurring. Then stop the errors from happening. You are going to create a ton of headaches as your code grows. – Joe Aug 05 '11 at 02:36
  • 1
    You should probably try to write your code so that you are confident that a variable is defined before using it, but if you can't, then possibly a better alternative instead of try/catch is to check if the variable exists, like `if (x) { }` or `if (typeof(x) != "undefined") { }` – Joe Enos Aug 05 '11 at 02:37
  • `undefined` is a valid value for a variable to have, and not automatically an error condition. Use an `if` statement like others have suggested. I don't know if I'd use the word "never", but I would say that using `try/catch` for this purpose is _almost_ never a good idea. Even aside from the speed-of-execution issues (which you may or may not care about) a `try/catch` is much clunkier to incorporate into your code. – nnnnnn Aug 05 '11 at 02:46

6 Answers6

2

If you're in a browser, you can test for global variables using window.foo. Eg:

if (window.foo === undefined)
    console.log("foo is not defined");

If I was reading code, I would prefer to read this than try { foo } catch { … }.

Also, note the triple equals: that is necessary because, if window.foo is null, then window.foo == undefined will be true, while window.foo === undefined will be false (what you want).

David Wolever
  • 148,955
  • 89
  • 346
  • 502
  • It still creates an error when the variable doesn't exist. So it doesn't serve the purpose I was looking for. – Brandon Lebedev Aug 07 '11 at 04:45
  • If I write `if (cowbell === undefined) { alert ("cow") }`, Firebug alerts "cowbell is not defined", and Firefox doesn't show the "cow" alert. Safari acts similarly. Its console shows "ReferenceError: Can't find variable: cowbell". That's why I went with Joe's suggestion. It's the version so far that executes whether or not the variable exists. – Brandon Lebedev Aug 07 '11 at 17:19
  • That's because `cowbell` isn't defined. However, if you use `window.cowbell`, as I suggest in my answer, you'll get the results you expect. – David Wolever Aug 08 '11 at 00:30
1

Personally, I try to avoid using try/catch statements if there is a simpler solution.

In your case, JS and every other language provides an easier way to see if a variable is defined.

if (window.x === undefined)

or if the variable was defined using var x:

if (x === undefined)
Jordan
  • 71
  • 1
  • 3
1

To test if a regular variable exists, I'd say your best bet is to compare the type to undefined, something like:

if (typeof(x) != "undefined") {
    // your variable exists
}
Joe Enos
  • 39,478
  • 11
  • 80
  • 136
0

I wouldn't use try/catch in that circumstance. If it complained that a variable was undefined I'd look for where the bug was and fix it.

MRAB
  • 20,356
  • 6
  • 40
  • 33
0

Generally, exceptions serve to notify the application that the assumption made by programmer was not satisfied. For example - network connection didn't work, file did not exist, security did not allow something, etc.

If you are aware of the fact that some variable may be undefined, then you should not use exceptions, rather handle that in an if. Or follow a strategy to have meaningful object or value whenever possible.

Ondra Žižka
  • 43,948
  • 41
  • 217
  • 277
0

Check the variable is defined using an if statement

Actually try catch is a very ineffecient way of testing if a variable is undefined. The best way is to check if the variable is defined in an if statement.

if(!someVariable === undefined){
  // Do Logic Here that requires someVariable to be defined
}
Justin Shield
  • 2,390
  • 16
  • 12