How to check if a variable or object is undefined?

前端 未结 7 1584
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:51

    You can use : if( typeof jQuery !== 'undefined')

    or

    Do what is recommended by mozilla

    if('jQuery' in window)

    https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/undefined

提交回复
热议问题