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?
outis
  • 75,655
  • 22
  • 151
  • 221
tarashish
  • 1,945
  • 21
  • 32
  • 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?](http://stackoverflow.com/questions/5076944/) – outis Jan 18 '12 at 11:22
  • Who told you that you could change undefined? – J. Holmes Jan 18 '12 at 11:22
  • For last question(s), see also [Why is there a `null` value in JavaScript?](http://stackoverflow.com/questions/461966/). – outis Jan 18 '12 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 – danwellman Jun 21 '14 at 14:30

4 Answers4

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.

outis
  • 75,655
  • 22
  • 151
  • 221
  • Note that, in Chrome 16/V8, the `undefined` property is set to writeable, so you still can't rely on it. – Andy E Jan 18 '12 at 11:48
  • @AndyE: at this point, I don't believe any browser is fully ES5 compliant, which is why I didn't mention any specific browsers. – outis Jan 18 '12 at 12:08
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.

Andy E
  • 338,112
  • 86
  • 474
  • 445
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        | ..
Anurag
  • 140,337
  • 36
  • 221
  • 257
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?

Community
  • 1
  • 1
Fabio Buda
  • 769
  • 2
  • 7
  • 16