130

I know this is really basic, but I am new to javascript and can't find an answer anywhere.

How can I check if a string is empty?

4
  • 4
    See stackoverflow.com/questions/154059/… Commented Mar 4, 2010 at 17:42
  • 1
    does whitespace count as empty? if so, you'll likely want a regex to test it. Commented Mar 4, 2010 at 17:42
  • 3
    Came here to add that some people should actually be checking for isBlank which would be variable.trim() === '' Commented May 6, 2019 at 0:40
  • Trimming a string always make sense if(title.trim().length === 0) { //code} Commented Jul 24, 2019 at 6:20

5 Answers 5

173

I check length.

if (str.length == 0) {
}
Sign up to request clarification or add additional context in comments.

10 Comments

I'm being a bit pedantic but if (!str || str.length === 0) would be preferable. (Note the 3 equals signs.) +1 to @Dustin though.
If you add blank spaces to your string, length counts the blank spaces, so with length we can't determine whether a string is empty or not.
@evolquez space is not same as empty, so is the case with null. space is a character so it can't be treated as "". OP is asking about empty string and I think this solution works well.
@ChrisNash I understand why === is better in general, but for this specific case, I can't find a reason where str.length would be zero due to type conversion. Are you using it as a convention or is there an example where you can get unexpected results?
Don't forget to trim if (!str || str.trim().length === 0)
|
60

If you want to know if it's an empty string use === instead of ==.

if(variable === "") {
}

This is because === will only return true if the values on both sides are of the same type, in this case a string.

for example: (false == "") will return true, and (false === "") will return false.

Comments

13

This should work:

if (variable === "") {

}

Comments

13

But for a better check:

if(str === null || str === '')
{
    //enter code here
}

4 Comments

Actually I was going to edit this before it got closed: if(!str || str == "") { //enter code here } There are many ways to do this, but a String is not necessarily null if it has "" in it. Empty is not null.
where did you learn that a null variable has a length of 0 in JS? What is the source where you found this info?
What if str is undefined? If you're checking falsiness, your best bet is if (!str). But if you're checking for empty string, as per the question, I think nxt's is the best. The accepted answer should be using ===, and if str is null/undefined, you get your classic Uncaught ReferenceError.
This answer is miscorrect because the condition gonna be truthy even if variable str is null, while null is an object type, not string.
-12
if (value == "") {
  // it is empty
}

4 Comments

If value is really a string, then this is correct, but it will also return true, if value = false or 0 or null. use === instead.
@Residuum: The question reads “How to check if a string is empty?”
@Gumbo: An answer that works under all circumstances is in my opinion better than an answer that provides the bare minimum, so I agree with Residuum. Things like that are especially important to point out for a beginner.
@Gumbo: As the question suggests, the questioner is not experienced in Javascript. And as Javascript is not a strongly typed language, it is a common source of error to not explicitly check for type as well. Been there, done that.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.