In ActionScript, how can you test if an object is defined, that is, not null?

- 30,738
- 21
- 105
- 131

- 1,091
- 2
- 11
- 14
-
1Does this work exactly the same in AS1, AS2 and AS3? – bzlm Feb 22 '09 at 11:04
4 Answers
test if an object is defined
This works in AS2 and AS3, and is the most reliable way to test if an object has a value.
if (obj != null) {
doSomethingWith(obj);
}
Its also the most reliable way to test an object's property and read it in the same expression:
if (arr[0] != null && arr[0]>5) {
doSomethingWith(arr[0]);
}
test if an object is null
There's a difference between null and undefined, but if you don't care you can just do a normal comparison between either one because they compare equal:
if (obj == null) {
doSomethingWith(obj);
}
is the same as
if (obj == undefined) {
doSomethingWith(obj);
}
If you care about the difference, use the === or !== operator, which won't convert them.
if (obj === undefined) {
// obj was never assigned a value
}
else if (obj === null) {
// obj was explicitly set to null
}
else {
doSomethingWith(obj);
}

- 110,798
- 141
- 398
- 607

- 101,441
- 24
- 103
- 129
-
1You mean all those if(obj == null || obj == undefined) statements in my codebase can be condensed? woo! :) – Herms Nov 18 '08 at 21:32
-
-
This is probably dead but got to this page googling for actionscript checing null. I have an mx:html control and on the htmlDOMInitialize I take the document like so: doc:Object = myMxHTML.domWindow.document; Then doc.head==null evaluates to false even though trace(doc.head) shows null. doc is not null, as in the documentation it can't be. – HMR Jan 16 '13 at 02:58
-
@HMR You might want to ask that as a new question about your specific situation. It sounds like there's something unusual going on, but I don't know enough about ActionScript to know what it is. – Matthew Crumley Jan 16 '13 at 13:34
-
Hi Matthew, I don't know what is supposed to happen either. Just this page turns up nr2 when searching for checking null so I wanted to make sure this is still current. Didn't do much testing but tracing notNullVar.nullOrNotExisting gave me null where notNullVar.nullOrNotExisting==null equals false. Could ask a new question but I bet a lot of people would end up here when googling for this and might be disappointed if the answers here won't work. – HMR Jan 16 '13 at 13:39
-
@HMR As far as I know, it's still current. It is for ECMAScript at least (but I don't know how closely ActionScript follows the spec). Host objects (like the DOM) are not specified by the language though, so they can generally behave however they want, which might explain the inconsistency you're seeing. – Matthew Crumley Jan 16 '13 at 13:44
For ActionScript 3.0, if all you want is a generic test for nothingness, then it's very easy:
var a;
var b;
var c;
var d;
a = undefined;
b = null;
c = 5;
if (a)
trace(a);
if (b)
trace(b);
if (c) // Will trace
trace(c);
if (d)
trace(d);
In the example above, only c
will trace. This is usually what I need, and just checking if (obj)
is the most readable version.
This method uses implicit conversion to a boolean value, also known as boolean coercion, and the details of what values will coerce to false and what values will coerce to true follow ECMA standards and are also documented specifically for ActionScript.

- 9,626
- 6
- 65
- 92
-
I thought I would have tried that, but this indeed works, and I think it looks nice too. – Matthew Shanley Feb 26 '09 at 01:49
-
I like it too, it makes the code more readable and makes refactoring easier. – bzlm Feb 26 '09 at 07:34
-
2
-
Eeek! This code can be **so much** cleaner! `var a = undefined, b = null, c = 5, d;` – Cilan Aug 20 '14 at 18:30
-
3@TheWobbuffet Why stop there? The code can be cleaned up to only consist of `trace(5)`. But that, as well as your suggestion, would defeat the purpose of the code, which was to illustrate as clearly as possible how boolean coercion works. :) – bzlm Aug 21 '14 at 12:14
Just test it against null.
var someObj:Object = getSomeObjectOrMaybeNull();
if(someObj == null) {
trace("someObj is null!");
} else {
trace("someObj is not null!");
}

- 37,540
- 12
- 78
- 101
You could also loop through a parent object to see if it contains any instances of the object you're looking for.
foundit=false;
for (var i in this) {
if (this[i]._name == "MyMovie") {
foundit=true;
}
}
-
This is very bad practice to loop just to find out if some object is null – David Salzer Dec 22 '11 at 09:41