I want to know what the difference is between null
and undefined
in JavaScript.
null and undefined are both are used to represent the absence of some value.
var a = null;
a is initialized and defined.
typeof(a)
//object
null is an object in JavaScript
Object.prototype.toString.call(a) // [object Object]
var b;
b is undefined and uninitialized
undefined object properties are also undefined. For example "x" is not defined on object c and if you try to access c.x, it will return undefined.
Generally we assign null to variables not undefined.
In JavaScript, undefined
means a variable has been declared but has not yet been assigned a value, such as:
var TestVar;
alert(TestVar); //shows undefined
alert(typeof TestVar); //shows undefined
null
is an assignment value. It can be assigned to a variable as a representation of no value:
var TestVar = null;
alert(TestVar); //shows null
alert(typeof TestVar); //shows object
From the preceding examples, it is clear that undefined
and null
are two distinct types: undefined
is a type itself (undefined) while null
is an object.
null === undefined // false
null == undefined // true
null === null // true
and
null = 'value' // ReferenceError
undefined = 'value' // 'value'
In JavasSript there are 5 primitive data types String , Number , Boolean , null and undefined. I will try to explain with some simple example
lets say we have a simple function
function test(a) {
if(a == null){
alert("a is null");
} else {
alert("The value of a is " + a);
}
}
also in above function if(a == null) is same as if(!a)
now when we call this function without passing the parameter a
test(); it will alert "a is null";
test(4); it will alert "The value of a is " + 4;
also
var a;
alert(typeof a);
this will give undefined; we have declared a variable but we have not asigned any value to this variable; but if we write
var a = null;
alert(typeof a); will give alert as object
so null is an object. in a way we have assigned a value null to 'a'
Both Null and undefined in JavaScript indicate absence of value.
var a = null; //variable assigned null value var b; // undefined
Despite the fact both exist for absence of value but: Undefined actually means the variable is not initialized. Functions that return nothing and function parameters for which no value is supplied, undefined value is returned. Use strict equality operator === to distinguish between null and undefined.
Reference: http://www.thesstech.com/javascript/null-and-undefined
(adding characters due to lack of characters, so I'm allowed to post this.)
When you declare a variable in javascript, it is assigned the value undefined
. This means the variable is untouched and can be assigned any value in future. It also implies that you don't know the value that this variable is going to hold at the time of declaration.
Now you can explicitly assign a variable null
. It means that the variable does not have any value. For example - Some people don't have a middle name. So in such a case its better to assign the value null to the middlename variable of a person object.
Now suppose that someone is accessing the middlename variable of your person object and it has the value undefined
. He wouldn't know if the developer forgot to initialize this variable or if it didn't have any value. If it has the value null
, then the user can easily infer that middlename doesn't have any value and it is not an untouched variable.