Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the benefit of untyped variable over an object? What is the difference between null and undefined?

According to this : http://help.adobe.com/en_US/ActionScript/3.0_ProgrammingAS3/WS5b3ccc516d4fbf351e63e3d118a9b90204-7f9f.html Quote:

An untyped variable is not the same as a variable of type Object. The key difference is that untyped variables can hold the special value undefined , while a variable of type Object cannot hold that value.

However when I test it as :


            var objTest:Object = 123;           
            var untypedTest:* = 123;

            objTest = undefined;
            untypedTest = undefined;            
            //This is understandable but why was the assignment even allowed?
            trace(objTest); // prints null
            trace(untypedTest); // prints undefined

            objTest=null;
            untypedTest = null;         
            //This is also understandable ... both can store null 
            trace(objTest); // prints null 
            trace(untypedTest); // prints null 

            //If they are null whey are they being equal to undefined? 
            if(objTest==undefined)
                trace("obj is undefined");
            if(untypedTest==undefined)
                trace("untyped is undefined");
            //Because null is same as undefined!
            if(null==undefined)
                trace("null is same as undefined?");


Two questions:

  • Why is assignment to undefined allowed for obj? (not a big issue since it still prints as null)
  • If we compare null with undefined the result true (even if null stored in an Object). What is the point of making a difference between null and undefined if they are equal?
like image 984
basarat Avatar asked Aug 08 '11 09:08

basarat


People also ask

What is difference between null and undefined?

Definition: Null: It is the intentional absence of the value. It is one of the primitive values of JavaScript. Undefined: It means the value does not exist in the compiler.

What is difference between null and undefined in JavaScript with example?

Unassigned variables are initialized by JavaScript with a default value of undefined. Here as the variable is declared but not assigned to any value, the variable by default is assigned a value of undefined. On the other hand, null is an object. It can be assigned to a variable as a representation of no value.

What's the difference between a variable that is null undefined or undeclared?

Null is pointing to nothing in memory. Undefined is a variable that has not been assigned any value. Lastly, undeclared is a variable that has not been properly declared using const, var, or let.

What is the difference between null and undefined stackoverflow?

In JavaScript, undefined is a type, whereas null an object. It means a variable declared, but no value has been assigned a value. Whereas, null in JavaScript is an assignment value.


1 Answers

  • Flash has type conversion to convert some types.

Some samples of that:

var i:int = NaN;
trace (i); // 0

Or:

var b:Boolean = null;
trace(b); // false

So when you're assigning undefined to Object instance Flash converts it to null the same way.

  • Your comparison applied type conversion on incompatible types before evaluating Boolean.

You can use strict comparison to have false:

if(null === undefined)
    trace("Never traced: null is not the same as undefined!");
like image 189
Constantiner Avatar answered Sep 28 '22 06:09

Constantiner