How to check if a variable or object is undefined?

前端 未结 7 1583
Happy的楠姐
Happy的楠姐 2020-12-10 16:51

I always thought I could just check an undefined var by comparing it to undefined, but this is the error I get in the chrome console :

7条回答
  •  感动是毒
    2020-12-10 17:30

    Process 1:

    if (jQuery) {  
        // jQuery is loaded  
    } else {
        // jQuery is not loaded
    }
    

    Process 2:

    if (typeof jQuery == 'undefined') {  
        // jQuery is not loaded  
    } else {
        // jQuery is loaded
    }
    

提交回复
热议问题