-1

Anyone please tell what is the difference between undefined and null in javascript and when I should use undefined and when null.

Faruk Hossen
  • 185
  • 1
  • 8

2 Answers2

2

undefined means a variable has been declared but has not yet been assigned. null is an assignment value. It can be assigned to a variable as a representation of no value

example.

var a;

alert(typeof(a));

var b = null;

alert(typeof(b));

Running the above script will result in the following output:

undefined object

——————————-

Nisha Salim
  • 687
  • 5
  • 13
0

undefined is not defined at all... means, that given variable does not exist.

null is defined variable that has value set to null (so basically you can say that it is defined variable with undefined value)

Flash Thunder
  • 11,672
  • 8
  • 47
  • 91