In Javascript i have read that we can define our own value for undefined
. Initially undefined == null
is true
.
- After we change the value of
undefined
willundefined == null
still betrue
? - By assigning our own value to
undefined
does it mean that we can assign real numbers to it? - Why are there two absence of value thingies like
null
andundefined
? Couldn't we just do with one? Is there any difference between them?
In Javascript i have read that we can define our own value for undefined
. Initially undefined == null
is true
.
- After we change the value of
undefined
willundefined == null
still betrue
? - By assigning our own value to
undefined
does it mean that we can assign real numbers to it? - Why are there two absence of value thingies like
null
andundefined
? Couldn't we just do with one? Is there any difference between them?
-
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? – outis Commented Jan 18, 2012 at 11:22 - Who told you that you could change undefined? – J. Holmes Commented Jan 18, 2012 at 11:22
-
For last question(s), see also Why is there a
null
value in JavaScript?. – outis 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 – danwellman Commented Jun 21, 2014 at 14:30
4 Answers
Reset to default 4undefined
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 parison 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 | ..
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.
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 pliant 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
.
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?