document.getelementbyId will return null if element is not defined?

前端 未结 3 867

In my code, I see this:

if (document.getElementById(\'xx\') !=null) {
    //do stuff
}

if xx element is not defined, will this

3条回答
  •  伪装坚强ぢ
    2020-12-29 19:13

    console.log(document.getElementById('xx') ) evaluates to null.
    
    document.getElementById('xx') !=null evaluates to false
    

    You should use document.getElementById('xx') !== null as it is a stronger equality check.

提交回复
热议问题