Martin Martin - 2 months ago 8
Javascript Question

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

What is the best way to test for an empty string with jquery-out-of-the-box, i.e. without plugins? I tried this:
http://zipalong.com/blog/?p=287

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.

Answer
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.