Summer Budget Travel Tips from Gadling

Null equals undefined, but null doesnt strictly equal undefined

null and undefinedI’ve been thinking about a post I saw on the 5 1/2 blog at the beginning of the month. They ran into a problem when running Boolean tests of ‘null === undefined’ inside ActionScript for a Flex application and couldn’t figure out what this traced to false and ‘null == undefined’ traces to true. In order to explain this we need to dive into the definitions of these two equality operators. The single equals sign (=) in ActionScript means ‘is assigned to.’ In other words, ‘x = 2’ means x is assigned the value of zero. The double equals sign (==) or equality operator means ‘is equal to’ and is used as a Boolean expression to see if the things on both sides of the operator have equal values regardless of type. In this case, ‘x == 2’ means ‘x has the same value as 2.’ The triple equals sign (===) or strict equality means ‘is exactly equal to’ and is used as a Boolean expression to see if things on both sides of the operator are equal in all respects (value and type). Here ‘x===2’ means ‘x has the same value as 2 and has the same type as two.’ (You could also get the same result by using the equality operator and the typeof operator).

But wait, how can we see this in action? Find out more after the jump.

Open a new FLA in flash and open the actions panel for the first frame of the project. Now enter the following:

var theNumber:Number;
theNumber = 2;
var theString:String;
theString = "2";
trace(theNumber == theString);
trace(theNumber === theString);
stop();

Now select the Control menu and choose Test movie to see what the trace statements display. Notice the Number ‘2’ has the same value as the String “2” but they are of different types hence the second evaluation to false.

What does this have to do with the problem of null equaling undefined but null not strictly equally undefined? Return to your FLA and change you ActionScript to the following:

trace(typeof(undefined));
trace(typeof(null));
stop();

Testing the movie will show that ‘undefined' has a value of ‘undefined’ and ‘null’ has a value of ‘null.’ Remember the definition of the strict equality operator. Based on the types of the two values alone they should not be strictly equal, but why are they considered equal via the equality operator (==)? To answer this we need to look at the basic values of null and undefined. Null is used to define a variable with no value or to define an empty set of values. Also null evaluated to 0 in a numeric sense Undefined if used to explain that a variable has no value before a specific value has been assigned or even before the variable has been declared. Both definitions are similar and thus equal in a broad sense but when carefully (or strictly) examined the two are not exactly equal.

Let’s end with a quick and dirty summary. Null is equal to undefined in value, but not type so null is not strictly equal to undefined. I know this is confusing, so if you have any comments or questions feel free to comment below. For more information on everything here check out the Macromedia LiveDocs: undefined, null, typeof, equality operators

via 5 1/2 blog

Reader Comments

(Page 1)
News
Macromedia (141)
Innovation (164)
Merger (41)
Releases (65)
WIN Business (24)
Events
MAX (26)
FlashForward (8)
MMUGs (16)
Webinars (4)
Learning
ActionScript (49)
Designer Certification (5)
Developer Certification (6)
Ideas (163)
Links (83)
Samples (57)
Tips and Tricks (98)
Tutorials (45)
Design & Development
Accessibility (14)
Ad Design (27)
Art (94)
Components (6)
Extensions (17)
Games (65)
Rich Internet Applications (99)
Sound (38)
Video (65)
Web Standards (33)
Software
Flash 8 (103)
Flash MX2004 (92)
Flash Paper 2 (3)
Flash Player 8 (55)
Flash MX2004 Professional (44)
Flash Lite (24)
Flash Platform (91)
Flash Remoting (3)
Flash Communications Server MX (8)
Flash Video (24)
Flash Paper (3)
Macromedia Contribute (2)
Macromedia Flex (8)
8Ball (15)
Maelstrom (19)
Software Updates (34)
Beta (22)
Bugs (14)
Studio 8 (25)

RESOURCES

RSS NEWSFEEDS

Powered by Blogsmith

Other Weblogs Inc. Network blogs you might be interested in: