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 for ""?

javascript null is-empty 
3 Answers

If you just want to check whether there's a truthy 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 === "") {
    //...
}

answered Jan 16


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,"") == ""){
}

answered Jan 16


var s; // undefined
var s = ""; // ""
s.length // 0

There's nothing representing an empty string in JavaScript. Do a check against either length (if you know that the var will always be a string) or against ""

answered Jan 16


Login and Submit Your Answer
Browse other questions tagged  javascript  null  is-empty or ask your own question.