views:

10555

answers:

8

Why null is considered an object in javascript?

Is checking

if ( object == null )
      do something

same as

if ( !object )
      do something

And also

What is the difference between null and undefined??

A: 

null is an object. It's type is null. undefined is not an object, it's type is undefined.

Vikas
wrong - both `null` and `undefined` are primitive values - `typeof null === 'object'` is a language bug, because `Object(null) !== null`
Christoph
No, is not. Object() cast work in that way, see http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf--15.2.2.1 new Object ( [ value ] )...8. (The argument value was not supplied or its type was Null or Undefined.)Create a new native ECMAScript object.The [[Prototype]] property of the newly constructed object is set to the Object prototype object.The [[Class]] property of the newly constructed object is set to "Object".The newly constructed object has no [[Value]] property.Return the newly created native object.
kentaromiura
@Christoph:I mean, you're right.null *should* be a type, what I means is that you cannot check it in that way because: alert(new Object() !== new Object()); /* true, new istance is not the same istance */alert(Object(null).constructor == {}.constructor); /* true as in spec */alert(Object(null).prototype == {}.prototype); /* true as in spec */alert(null instanceof Object); /* obviously false, null means not instantiated */But basically is a spec bug XDsee:http://www.uselesspickles.com/blog/2006/06/12/javascript-null-identity-crisis/and http://javascript.crockford.com/remedial.html
kentaromiura
+7  A: 

What is the difference between null and undefined??

A property when it has no definition, is undefined. null is an object. It's type is null. null is a special value meaning "no value. undefined is not an object, it's type is undefined.

You can declare a variable, set it to null, and the behavior is identical except that you'll see "null" printed out versus "undefined". You can even compare a variable that is undefined to null or vice versa, and the condition will be true:

 undefined == null
 null == undefined

Refer to JavaScript Difference between null and undefined for more detail.

and with your new edit yes

if (object == null)  does mean the same  if(!object)

when testing if object is false, they both only meet the condition when testing if false, but not when true

Check here: Javascript gotcha

TStamper
edited my post for the condition checking
rahul
You should use ===, then undefined !== null :D
olliej
pay attention to the last part, is incorrectsee my answer ;)
kentaromiura
!object is not the same as "object == null" ... In fact, they're quite different. !object will return true if is object is 0, an empty string, Boolean false, undefined or null.
J-P
Is null really an object? The 1st link you have provided checks null by typeof, but typeof(null) evaluates to 'object' because of an language design error.
c411
A: 

For example window.someWeirdProperty is undefined, so "window.someWeirdProperty === null" evaluates to false while "window.someWeirdProperty === undefined" evaluates to true.

Moreover checkif if (!o) is not the same as checking if (o == null) for o being false.

Piotr Findeisen
Can you elaborate the difference between the two conditions
rahul
read my answer, he means that if o is equals to 0, false or "" the Boolean value is false:var undef, various =[0,"",null,false,undef];for(var obj in various){ alert(!obj); //4 times false in IE,5 in FF ;)}
kentaromiura
+1  A: 
var x = null;

x is defined as null

y is not defined; // because I did not define it

if (!x)

null is evaluated as false

Chad Grant
A: 

null and undefined are both false for value equality (null==undefined): they both collapse to boolean false. They are not the same object (null!==undefined).

undefined is a property of the global object ("window" in browsers), but is a primitive type and not an object itself. It's the default value for uninitialized variables and functions ending without a return statement.

null is an instance of Object. null is used for DOM methods that return collection objects to indicate an empty result, which provides a false value without indicating an error.

Anonymous
+6  A: 

The difference can be summarize into this snippet:

alert(typeof(null));      // object
alert(typeof(undefined)); // undefined

alert(null !== undefined) //true
alert(null == undefined) //true

checking

object == null is different to check if ( !object )

the latter equals to ! Boolean(object) because the unary ! operator automatically cast the right operand into a Boolean

since Boolean(null) equals false then !false === true

so if your object is not null but false or 0 or "" the check will pass because

alert(Boolean(null)) //false
alert(Boolean(0))     //false
alert(Boolean(""))   //false

bye ;)

kentaromiura
+1, for the explanation of !Boolean(object)
c411
+29  A: 
(name is undefined)

You: What is name?
JavaScript: name? What's a name? I don't know what you're talking about. You haven't ever mentioned any name before. Are you seeing some other scripting language on the (client-)side?

name = null;

You: What is name?
JavaScript: I don't know.

In short; undefined is where no notion of the thing exists; it has no type, and it's never been referenced before in that scope; null is where the thing is known to exist, but it's not known what the value is.

One thing to remember is that null is not, conceptually, the same as false or "" or such, even if they equate after type casting, i.e.

name = false;

You: What is name?
JavaScript: Boolean false.

name = '';

You: What is name?
JavaScript: Empty string

Rob
But in JavaScript, the empty string '' is still a boolean false.
Andreas Grech
what a funny way to explain
Jake
+1  A: 

Some precisions,

null and undefined ARE two different values. One representing the absence of value for a name and the other representing the absence of a name. Douglas, if you're out there, correct me if I'm wrong!


What happens in an if goes as follow for if( o ) :

The expression in the parentheses o is evaluated, then the if kicks in type-cohersing the value of the expression in the parentheses - in our case o.

Falsy ( that will get cohersed to false ) values in javascript are: '', null, undefined, 0, false.

Laurent Villeneuve