What is the difference between null and undefined in JavaScript?

前端 未结 30 3319
夕颜
夕颜 2020-11-21 23:06

I want to know what the difference is between null and undefined in JavaScript.

30条回答
  •  忘了有多久
    2020-11-21 23:51

    Undefined means a variable has been declared but has no value:

    var var1;
    alert(var1); //undefined
    alert(typeof var1); //undefined
    

    Null is an assignment:

    var var2= null;
    alert(var2); //null
    alert(typeof var2); //object
    

提交回复
热议问题