+ 2
What is the difference between null and undefined in JavaScript ?
3 Respostas
+ 9
A null represents nothing. Its not an actual value.If you are try to reference a null variable you will get an error.
However Undefined is an actual value. It means the name in the current scope is bound to any object.
+ 3
If you declare a var but do not assign a value, this is undefined.
var x; // undefined
Null is an assignment value. It can be assigned to a variable as a representation of no value.
+ 3
@noname is right, @Anand Krishna is wrong and confusing ^^
undefined: not a real value, but a special type of variable wich tell that the variable doesn't exist (wasn't declared/assigned -- assignment will implicitly declare variable, as global var if 'var' keyword avoided, no matter the variable scope where the assignment occurs)
null: a special value and type of variable wich tell that the variable exists but is assigned with 'no value' (quite different of 'not defined': var was declared, and explicitly assigned with 'none value' -- empty: this will be useful usualy for number to make a difference between 0 and nothing ;P)