90

I want to try to do string call equivalent to the C# String.IsNullOrEmpty(string) in javascript. I looked online assuming that there was a simple call to make, but I could not find one.

For now I am using a if(string === "" || string === null) statement to cover it, but I would rather use a predefined method (I keep getting some instances that slip by for some reason)

What is the closest javascript (or jquery if then have one) call that would be equal?

Daniele Armanasco
  • 7,289
  • 9
  • 43
  • 55
Soatl
  • 10,224
  • 28
  • 95
  • 153
  • possible duplicate of [What is the best way to test for an empty string with jquery-out-of-the-box?](http://stackoverflow.com/questions/1812245/what-is-the-best-way-to-test-for-an-empty-string-with-jquery-out-of-the-box) – Daniel A. White Apr 21 '11 at 16:28
  • 3
    @DanielA.White the argument is the same but, for a C# developer like me, this question (with its title) is more likely to be found – Daniele Armanasco Sep 07 '13 at 15:58

9 Answers9

229

You're overthinking. Null and empty string are both falsey values in JavaScript.

if(!theString) {
 alert("the string is null or empty");
}

Falsey:

  • false
  • null
  • undefined
  • The empty string ''
  • The number 0
  • The number NaN
JoJo
  • 19,587
  • 34
  • 106
  • 162
  • 19
    Amazing this got so many up votes. This answer is incorrect. You said yourself that a falsey value would be 0. If the user types 0 into a field this is not an empty string. An empty string is literally an "empty" string. – KingOfHypocrites Mar 19 '15 at 19:28
  • 6
    If the user type 0, the value will be "0" string not number 0. So it will still be true I guess. Please post your best solution, KingOfHypocrites? – Jonas T Mar 27 '15 at 06:06
  • 2
    KingOfHypocrites is right in that the string "0" will give an incorrect result: "0" == false returns true !"0" == false returns true – Martien de Jong May 23 '16 at 12:02
  • 2
    Useful. Know your dataset though. If "0" or "false" are possible inputs, then this solution might not always work. – Nevyn May 30 '18 at 20:04
  • @KingOfHypocrites It got so many upvotes because this is the reasonable solution most people who come here are looking for rather than the perfectly accurate answer that handles every edge case that they don't care about. – BVernon Nov 22 '22 at 15:57
  • @BVernon If you are a lazy programmer who can't type a few extra characters to prevent a potential bug in a professional application, it's a great solution. – KingOfHypocrites Nov 25 '22 at 14:19
  • @KingOfHypocrites Illogical argument. The point is that this is a reasonable solution for the majority of cases where we already know for certain those edge cases aren't possible. And it's not lazy to write short, succinct, readable code that works. I also happen to agree with SO founder Jeff Atwood that "code is the problem". Less code is better code. I 100% expect you to disagree though so we can agree to disagree. ;) – BVernon Nov 28 '22 at 07:54
13

If, for whatever reason, you wanted to test only null and empty, you could do:

function isNullOrEmpty( s ) 
{
    return ( s == null || s === "" );
}

Note: This will also catch undefined as @Raynos mentioned in the comments.

Code Maverick
  • 20,171
  • 12
  • 62
  • 114
7

If you know that string is not numeric, this will work:

if (!string) {
  .
  .
  .
awm
  • 6,526
  • 25
  • 24
  • The string "0" is falsey, so you don't have to check if the string is numeric. – JoJo Apr 21 '11 at 17:49
  • 1
    Yes, the string "0" is truthy, but if variable `string` is in fact not a string but the *Number* zero, then `string` will be falsey. It won't be an issue if we know that `string` is either an actual string or null. – awm Apr 22 '11 at 01:10
7
if (!string) {
  // is emtpy
}

What is the best way to test for an empty string with jquery-out-of-the-box?

Community
  • 1
  • 1
alexl
  • 6,841
  • 3
  • 24
  • 29
4

You can create one Utility method which can be reused in many places such as:

 function isNullOrEmpty(str){
    var returnValue = false;
    if (  !str
        || str == null
        || str === 'null'
        || str === ''
        || str === '{}'
        || str === 'undefined'
        || str.length === 0 ) {
        returnValue = true;
    }
    return returnValue;
  }
Anjum....
  • 4,086
  • 1
  • 35
  • 45
3

you can just do

if(!string)
{
  //...
}

This will check string for undefined, null, and empty string.

BrokenGlass
  • 158,293
  • 28
  • 286
  • 335
1

To be clear, if(!theString){//...} where theString is an undeclared variable will throw an undefined error, not find it true. On the other hand if you have: if(!window.theString){//...} or var theString; if(!theString){//...} it will work as expected. In the case where a variable may not be declared (as opposed to being a property or simply not set), you need to use: if(typeof theString === 'undefined'){//...}

My preference is to create a prototype function that wraps it up for you.

Craig
  • 150
  • 2
  • 6
1

Since the answer that is marked as correct contains a small error, here is my best try at coming up with a solution. I have two options, one that takes a string, the other takes a string or a number, since I assume many people are mixing strings and numbers in javascript.

Steps: -If the object is null it is a null or empty string. -If the type is not string (or number) it's string value is null or empty. NOTE: we might throw an exception here as well, depending on preferences. -If the trimmed string value has a length that is small than 1 it is null or empty.

var stringIsNullOrEmpty = function(theString)
{
    return theString == null || typeof theString != "string" || theString.trim().length < 1;
}

var stringableIsNullOrEmpty = function(theString)
{
    if(theString == null) return true;
    var type = typeof theString;
    if(type != "string" && type != "number") return true;
    return theString.toString().trim().length < 1;
}
Martien de Jong
  • 731
  • 1
  • 7
  • 19
1

you can say it by logic

Let say you have a variable name a strVal, to check if is null or empty

if (typeof (strVal) == 'string' && strVal.length > 0) 
{
// is has a value and it is not null  :)
}
else
{
//it is null or empty :(
}
Ahmad Hindash
  • 1,519
  • 15
  • 16