JavaScript check if variable exists (is defined/initialized)

前端 未结 29 1688
孤城傲影
孤城傲影 2020-11-22 00:59

Which method of checking if a variable has been initialized is better/correct? (Assuming the variable could hold anything (string, int, object, function, etc.))



        
29条回答
  •  Happy的楠姐
    2020-11-22 01:28

    It is difficult to distinguish between undefined and null. Null is a value you can assign to a variable when you want to indicate that the variable has no particular value. Undefined is a special value which will be the default value of unassigned variables.

    
    var _undefined;
    var _null = null;
    
    alert(_undefined); 
    alert(_null); 
    alert(_undefined == _null);
    alert(_undefined === _null);
    
    

提交回复
热议问题