JavaScript check if variable exists (is defined/initialized)

前端 未结 29 1160
孤城傲影
孤城傲影 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条回答
  •  一生所求
    2020-11-22 01:23

    if (typeof console != "undefined") {    
       ...
    }
    

    Or better

    if ((typeof console == "object") && (typeof console.profile == "function")) {    
       console.profile(f.constructor);    
    }
    

    Works in all browsers

提交回复
热议问题