tarashish
tarashish

Reputation: 1965

Null and undefined in javascript

In Javascript i have read that we can define our own value for undefined. Initially undefined == null is true.

  1. After we change the value of undefined will undefined == null still be true?
  2. By assigning our own value to undefined does it mean that we can assign real numbers to it?
  3. Why are there two absence of value thingies like null and undefined? Couldn't we just do with one? Is there any difference between them?

Upvotes: 0

Views: 3063

Answers (4)

Anurag
Anurag

Reputation: 141899

undefined is a variable on the global object which is window in browser environments. Its initial value is the primitive undefined.

Being a property on the global object, historically you could have changed its value as,

window.undefined = "42"; // or just undefined = "42"

The meaning of life is now clearly defined. But since EcmaScript-5 is out, this has been disallowed, and even though it is still a property of the global object, it has been made non-writable now.

The primitives null and undefined are not the same thing if no tampering has occurred.

Null is a data type that has the sole value null. Undefined is another data type whose sole value is the primitive undefined. You can verify whether they represent the same object or not easily.

null === undefined // false

However,

null == undefined // true

is true, because they are both casted to the boolean value false before a comparison is made. The rules for converting both these values to boolean are clearly defined in section 9.2 of the spec.

9.2 ToBoolean

Argument Type | Result
-------------------------------------------------------------------------
Undefined     | false
Null          | false
Boolean       | The result equals the input argument (no conversion).
Number        | ..
String        | ..
Object        | ..

Upvotes: 4

outis
outis

Reputation: 77450

Note that in older JS versions, undefined isn't a keyword the way null is. You can define a value for undefined because it's just a variable.

var null; // syntax error
null = 0; // another error
var undefined; // not an error
undefined = null; //not an error

However, doing this is a bad idea, as it could break things in 3rd party code that you use. ECMAScript 5 defines a read-only undefined property on the global object (note: still not a keyword), so you can't assign to it in ES5 compliant JS implementations.

As for whether undefined == null after assigning a value to null, just like foo == null it depends entirely on the value you assign to undefined.

Upvotes: 4

Andy E
Andy E

Reputation: 344733

But after we change the value of undefined will undefined == null still be true?

That depends what you change it to. If you change it to null, then yes. If you change it to anything else, then no. Just don't change it.

And by assigning our own value to undefined does it mean that we can assign real numbers to it?

Well, yes. Didn't you try it?

Lastly why are there two absence of value thingies like null and undefined? Couldn't we just do with one? Is there any difference between them?

undefined means that a value has not been assigned or there's no defined value. null means that there is a value, but that value is null. So, yes, there's a difference.

Upvotes: 4

Fabio Buda
Fabio Buda

Reputation: 769

In javascript undefined means a declared variable but not yet assigned value.

Null is a value so a var = null is a defined variable.

Try to read this What is the difference between null and undefined in JavaScript?

Upvotes: 1

Related Questions