title: Actionscript3: Does variable exist?
body: <p>I'm a little bit new to Actionscript, but I can't figure this one out. I've done a lot of searching on this topic and haven't found a clear answer. I've tried the following solutions that people posted online but none of them work.</p>
  
  <p>All the following <em>solutions</em> give the error: <em>1120: Access of undefined property myVariable</em></p>
  
  <p><strong>Suggestion #1:</strong></p>
  
  <pre><code>try {
       trace(myVariable); }
  catch {
       trace("your variable doesn't exist"); }
  </code></pre>
  
  <p><strong>Suggestion #2:</strong></p>
  
  <pre><code>if (myVariable) {
       trace("your variable exists!!"); }
  else {
       trace("it doesn't exist"); }
  </code></pre>
  
  <p><strong>Suggestion #3:</strong></p>
  
  <pre><code>if ( myVariable == null )
       trace("your variable doesn't exist");
  </code></pre>
  
  <p><strong>Suggestion #4:</strong></p>
  
  <pre><code>if ( myVariable == undefined )
       trace("your variable doesn't exist");
  </code></pre>
  
  <p>Like I said, I've found many forums posts and stuff online that give the above suggestions saying they will work, but they all seem to be giving me that same <em>1120: Access of undefined property myVariable</em> error.</p>
  
  <p>By the way, in case you are wondering why I would need to check if a variable exists or not, I'm planning on passing variables to the SWF in its URL, so I need to make sure the proper variables exist and handle the code properly if they are not passed in.</p>
  
  <hr>
  
  <p>Thanks for the quick reply. Still not really working. The scope of the variable is just on the top/root level of the script. Basically, I start a new flash file, on the first frame I add the following action:</p>
  
  <pre><code>// to check for this.myVariable
  if ( this.hasOwnProperty( "myVariable" ) ) {
       trace("myVariable exists");
  }
  else
  {
       //Variable doesn't exist, so declare it now
       trace("declaring variable now...");
       var myVariable = "Default Value";
  }
  
  trace(myVariable);
  </code></pre>
  
  <p>When I run the flash file, I get this output:</p>
  
  <pre><code>myVariable exists
  undefined
  </code></pre>
  
  <p>I was expecting this:</p>
  
  <pre><code>declaring variable now...
  Default Value
  </code></pre>