How to check 'undefined' value in jQuery

后端 未结 11 567
慢半拍i
慢半拍i 2020-12-12 11:49

Possible Duplicate:
Detecting an undefined object property in JavaScript
javascript undefined compare

H

相关标签:
11条回答
  • 2020-12-12 12:11

    I am not sure it is the best solution, but it works fine:

    if($someObject['length']!=0){
        //do someting
    }
    
    0 讨论(0)
  • 2020-12-12 12:16
    if (value === undefined) {
        // ...
    }
    
    0 讨论(0)
  • 2020-12-12 12:20

    In this case you can use a === undefined comparison: if(val === undefined)

    This works because val always exists (it's a function argument).

    If you wanted to test an arbitrary variable that is not an argument, i.e. might not be defined at all, you'd have to use if(typeof val === 'undefined') to avoid an exception in case val didn't exist.

    0 讨论(0)
  • 2020-12-12 12:24

    Note that typeof always returns a string, and doesn't generate an error if the variable doesn't exist at all.

    function A(val){
      if(typeof(val)  === "undefined") 
        //do this
      else
       //do this
    }
    
    0 讨论(0)
  • 2020-12-12 12:24

    I know I am late to answer the function but jquery have a in build function to do this

    if(jQuery.type(val) === "undefined"){
        //Some code goes here
    }
    

    Refer jquery API document of jquery.type https://api.jquery.com/jQuery.type/ for the same.

    0 讨论(0)
提交回复
热议问题