257

What is the best way to test for an empty string with jquery-out-of-the-box, i.e. without plugins? I tried this.

But it did't work at least out-of-the-box. It would be nice to use something that's builtin.

I wouldn't like to repeat

if (a == null || a=='')

everywhere if some if (isempty(a)) would be available.

2

10 Answers 10

605
if (!a) {
  // is emtpy
}

To ignore white space for strings:

if (!a.trim()) {
    // is empty or whitespace
}

If you need legacy support (IE8-) for trim(), use $.trim or a polyfill.

Sign up to request clarification or add additional context in comments.

16 Comments

@CoffeeAddict empty string is falsy in javascript, so yes, the expression evaluates to true if a is "" or any other falsy value (null, false, undefined, 0, NaN).
If you need to also match strings with only whitespace, it's a good idea to use if (!$.trim(a))
In my experience the vast majority of times you want to run this test you also want it to include testing for whitespace. Since that is the usual-case, OP should include in his answer the comment from @IlariKajaste
The accepted answer will fail when you input a 0, however it will work for 1, 2, 3, 4 etc.
Isn't if (!a.trim()) a bit dangerous? What if a is undefined or null?
|
30

The link you gave seems to be attempting something different to the test you are trying to avoid repeating.

if (a == null || a=='')

tests if the string is an empty string or null. The article you linked to tests if the string consists entirely of whitespace (or is empty).

The test you described can be replaced by:

if (!a)

Because in javascript, an empty string, and null, both evaluate to false in a boolean context.

4 Comments

if(!a) wont it fail for a string consisting of say 4 spaces ? ` `
@KNU Yes, but the question asks about an empty string, and a string comprising of spaces is not an empty string. See what I wrote about the difference between what was asked and what the linked code does
This answer is wrong. It treats '0' like an empty string, which it isn't.
@JohnHenckel: It'll treat 0 like an empty string, but not "0". I assume that's what you meant? It's conceivable that this is being used in a situation where you know that a is either a string, or null, but yes. Something to be aware of.
28

Based on David's answer I personally like to check the given object first if it is a string at all. Otherwise calling .trim() on a not existing object would throw an exception:

function isEmpty(value) {
  return typeof value == 'string' && !value.trim() || typeof value == 'undefined' || value === null;
}

Usage:

isEmpty(undefined); // true
isEmpty(null); // true
isEmpty(''); // true
isEmpty('foo'); // false
isEmpty(1); // false
isEmpty(0); // false

1 Comment

i check with <br /><p></p> from textarea and it said not empty as it should
6

Check if data is a empty string (and ignore any white space) with jQuery:

function isBlank( data ) {
    return ( $.trim(data).length == 0 );
}

Comments

5
if(!my_string){ 
// stuff 
}

and

if(my_string !== "")

if you want to accept null but reject empty

EDIT: woops, forgot your condition is if it IS empty

Comments

2

Try executing this in your browser console or in a node.js repl.

var string = ' ';
string ? true : false;
//-> true

string = '';
string ? true : false;
//-> false

Therefore, a simple branching construct will suffice for the test.

if(string) {
    // string is not empty
}

1 Comment

it's better to say "!!string" instead of "string ? true : false" or even just "string".
2

Since you can also input numbers as well as fixed type strings, the answer should actually be:

function isBlank(value) {
  return $.trim(value);
}

1 Comment

Shouldn't it be like this? return !!$.trim(value);
0

Please see this code by jQuery:

 if ($("#ItemId").val().trim() != "") {
            console.log($("#ItemId").val());
        }

Comments

-2

Try this

if(!a || a.length === 0)

1 Comment

The OP specifically stated that they are looking for an alternative to this.
-4
if((a.trim()=="")||(a=="")||(a==null))
{
    //empty condition
}
else
{
    //working condition
}

1 Comment

If a is null then a.trim() would fail. This is a bad order to check things. And if you change the order, your answer does not provide anything, that has not been suggested before.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.