admin管理员组

文章数量:1134247

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?

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?

Share Improve this question edited Sep 7, 2013 at 16:10 Daniele Armanasco 7,45110 gold badges45 silver badges58 bronze badges asked Apr 21, 2011 at 16:23 SoatlSoatl 10.6k28 gold badges100 silver badges156 bronze badges 2
  • possible duplicate of What is the best way to test for an empty string with jquery-out-of-the-box? – Daniel A. White Commented Apr 21, 2011 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 Commented Sep 7, 2013 at 15:58
Add a comment  | 

9 Answers 9

Reset to default 234

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

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.

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

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

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

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;
  }

you can just do

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

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

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.

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;
}

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 :(
}

本文标签: jqueryC StringIsNullOrEmpty Javascript equivalentStack Overflow