2017  Kodetalk | Feedback | Privacy Policy | Terms | About
userimage

What is the difference between null and undefined in javascript

We are using null and undefined in javascript and i am not sure which cases i should go for null and which case i should go for undefined. What is the difference between null and undefined in javascript.

userimage
In javaScript null and undefined is nothing but an alternative of nothing.


undefined in Javascript


undefined means basically value of the variable is not defined. In javaScript it has a global variable undefined whose value is "undefined" and typeof undefined is also "undefined". Undefined is not a constant or a keyword. undefined is a type with exactly one value: undefined. Assigning a new value to it does not change the value of the type undefined.


Cases of undefined:


A declared variable without assigning any value to it.
Implicit returns of functions due to missing return statements.
return statements that do not explicitly return anything.
Lookups of non-existent properties in an object.
Function parameters that have not passed.
Anything that has been set to the value of undefined.
Any expression in the form of void(expression)
The value of the global variable undefined


null in Javascript


null means empty or non-existent value which is used by programmers to indicate "no value". null is a primitive value and you can assign null to any variable. null is not an object, it is a primitive value. For example, you cannot add properties to it. Sometimes people wrongly assume that it is an object, because typeof null returns "object".

Answer is