开发者

What the best way to check arr, object, string with JQuery undefined + null+ trim(str)!==""?

1. What the best way to check array or object with JQuery undefined + null?

I check array like that:

function f1(arr){
    开发者_如何学运维if(arr!==undefined&&arr!=null){
      //code
    }
}

Is Jquery have better way?


2. What the best way to check String with JQuery trim(str)!==""?

I check String like that:

function f2(str){
    if(str!==undefined&&str!=null&&$.trim(str)!==''){
      //code
    }
}

Is Jquery have better way?

Thanks


  1. if ( $.isArray( arr ) ) { alert('is array'); }

  2. if ( $.trim(str) != '' ) { alert('is non empty string'); }

Testing:

$.isArray({})
false
$.isArray('')
false
$.isArray(null)
false
$.trim(null)
""
$.trim( undefined )
""

EDIT: You can probably be more explicit in test #2 if you use typeof.

if ( (typeof str === 'string') && ($.trim(str ) != '') ) {

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜