0

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?
4
  • The first question can't be answered unless you say what you set the value of the variable undefined to. For the last question(s), see What is the difference between null and undefined in JavaScript? Commented Jan 18, 2012 at 11:22
  • Who told you that you could change undefined? Commented Jan 18, 2012 at 11:22
  • For last question(s), see also Why is there a null value in JavaScript?. Commented Jan 18, 2012 at 11:38
  • undefined is not a variable like many of the below answers state. Just because in older versions of ECMA script it is writable, and just because it was a valid identifier in older versions of ECMA script, does not make it a variable. It is a data type Commented Jun 21, 2014 at 14:30

4 Answers 4

4

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        | ..
Sign up to request clarification or add additional context in comments.

2 Comments

Note that, in Chrome 16, window.undefined is writeable. In 18, it's not (not sure about 17).
@AndyE - thanks for the heads up. Just tested in Chrome 17 - its not-writable.
4

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.

Comments

4

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.

2 Comments

Note that, in Chrome 16/V8, the undefined property is set to writeable, so you still can't rely on it.
@AndyE: at this point, I don't believe any browser is fully ES5 compliant, which is why I didn't mention any specific browsers.
1

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?

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.