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

后端 未结 30 4139
长发绾君心
长发绾君心 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 just want to check whether there's any value, you can do

    if (strValue) {
        //do something
    }
    

    If you need to check specifically for an empty string over null, I would think checking against "" is your best bet, using the === operator (so that you know that it is, in fact, a string you're comparing against).

    if (strValue === "") {
        //...
    }
    

提交回复
热议问题