126

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?

Community
  • 1
  • 1
Andrew
  • 227,796
  • 193
  • 515
  • 708

5 Answers5

173

I check length.

if (str.length == 0) {
}
Dustin Laine
  • 37,935
  • 10
  • 86
  • 125
  • 55
    I'm being a bit pedantic but `if (!str || str.length === 0)` would be preferable. (Note the 3 equals signs.) +1 to @Dustin though. – intrepidis Jun 21 '13 at 14:46
  • 13
    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. – avolquez Aug 28 '13 at 12:21
  • 9
    @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. – Fr0zenFyr Jul 04 '14 at 04:57
  • 1
    @ChrisNash Why would you need `===` instead of `==` in this specific case? Is it possible for `str.length` to return something other than a numeric value that might evaluate to `falsy`? – user Sep 11 '14 at 11:16
  • @buffer This explains it all: http://stackoverflow.com/questions/359494/does-it-matter-which-equals-operator-vs-i-use-in-javascript-comparisons – intrepidis Sep 11 '14 at 19:57
  • 3
    @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? – user Sep 12 '14 at 05:05
  • var str = " "; now how you will check ???? – insCode Sep 08 '16 at 08:10
  • 9
    Don't forget to trim `if (!str || str.trim().length === 0)` – Jake Pucan Feb 27 '19 at 07:54
  • @intrepidis not pedantic at all, that's core, foundational js. You just saved one some unnecessary aggravation. This answer as it stands is small disaster waiting to happen. – MrBoJangles Jul 09 '19 at 16:39
  • so why `.length == 0` is better than `=== ""` ??? I need an answer. – Vladimir Kovalchuk Apr 28 '21 at 13:58
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.

nxt
  • 1,983
  • 12
  • 13
11

This should work:

if (variable === "") {

}
Tom Castle
  • 2,198
  • 1
  • 16
  • 20
11

But for a better check:

if(str === null || str === '')
{
    //enter code here
}
JeanValjean
  • 17,172
  • 23
  • 113
  • 157
Christopher Richa
  • 1,278
  • 1
  • 9
  • 20
  • 3
    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. – Christopher Richa Mar 04 '10 at 18:35
  • 3
    where did you learn that a null variable has a length of 0 in JS? What is the source where you found this info? – PositiveGuy Nov 07 '11 at 07:07
  • 1
    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. – MrBoJangles Jul 09 '19 at 16:37
  • This answer is miscorrect because the condition gonna be truthy even if variable `str` is `null`, while `null` is an `object` type, not `string`. – Nickensoul Aug 19 '20 at 17:04
-13
if (value == "") {
  // it is empty
}
Ray
  • 21,485
  • 5
  • 48
  • 64
  • 10
    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 Mar 04 '10 at 17:43
  • 2
    @Residuum: The question reads “How to check if a string is empty?” – Gumbo Mar 04 '10 at 17:44
  • 3
    @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. – OregonGhost Mar 04 '10 at 17:47
  • 2
    @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. – Residuum Mar 04 '10 at 17:48