How can I check for an empty/undefined/null string in JavaScript?

后端 未结 30 4141
长发绾君心
长发绾君心 2020-11-21 23:47

I saw this question, but I didn\'t see a JavaScript specific example. Is there a simple string.Empty available in JavaScript, or is it just a case of checking f

30条回答
  •  忘掉有多难
    2020-11-21 23:58

    If you need to make sure that the string is not just a bunch of empty spaces (I'm assuming this is for form validation) you need to do a replace on the spaces.

    if(str.replace(/\s/g,"") == ""){
    }
    

提交回复
热议问题