views:

44483

answers:

15

I saw this thread, but I didn't see a JavaScript specific example. Is there a simple string.Empty in JavaScript, or is it just checking for "" ?

+2  A: 

I usually compare to "" in Javascript.

toast
+86  A: 

If you just want to check whether there's any value, you can do

if (strValue) {
    //do something
}

If you need to check specifically for an empty string over null, I would think checking against "" is your best bet, using the === operator (so that you know that it is, in fact, a string you're comparing against).

bdukes
Excellent answer, well written.
Raithlin
(Would be even better if you mentioned that === also checks that the two are the same type)
Chris Noe
Testing the length property may actually be faster than testing the string against "", because the interpreter won't have to create a String object from the string literal.
Vincent Robert
@Vincent doing some naïve profiling in Chrome developer tools, testing `=== ''` vs `.length` didn't show any discernible improvement (and using `.length` only works if you can assume that you have a string)
bdukes
@bdukes when you start to care about that kind of micro-optimizations, I don't think Chrome is the browser where you are having most of your performance problems...
Vincent Robert
Point taken, I just don't have the motivation to figure out how to profile something like that in IE6...
bdukes
+1  A: 

I would say check against "".

Christophe Herreman
+9  A: 

I would not worry too much about the most efficient method. Use what is most clear to your intention. For me that's usually strVar == "".

EDIT: per comment from Constantin, if strVar some how ended up containing an integer 0 value, then strVar == "" returns true. So if that is a danger in your situation, you should code strVar === "", which ensures that the types are also the same.

Chris Noe
Bad idea. You'll get true if strVar is accidentally assigned 0.
Constantin
+3  A: 
var s; // undefined
var s = ""; // ""
s.length // 0

There's nothing representing an empty string in JavaScript. Do a check against either length (if you know that the var will always be a string) or against ""

roosteronacid
+5  A: 

The closest thing you can get to str.Empty (with the precondition that str is a String) is:

if (!str.length) { ...
Ates Goral
the best desicion infact!!
holms
+16  A: 

If you need to make sure that the string is not just a bunch of empty spaces (I'm assuming this is for form validation) you need to do a replace on the spaces.

if(str.replace(/\s/g,"") == ""){
}
Sugendran
Now that's an expensive test
Chris Noe
But does the job if what you actually want to test for is a string with non-space content. Is there a less-expensive way to test this?
flashparry
A: 

just FYI, i think the most useful APIs for the String class are at Mozilla and javascript kit. elated.com has a tutorial on all of String's properties, methods,...

Gene T
A: 

thtx i'was searching for this only , solved my prob :
by this

if(str.replace(/\s/g,"") == ""){
}
A: 

I usually use something like:

if (str == "") {
     //Do Something
}
else {
     //Do Something Else
}
jmc734
A: 

you could also go with regexps:

if((/^\s*$/).test(str)) { }

Checks for strings that are either empty or filled with whitespace.

oem
A: 

I use :

function empty(e) {
                    switch(e) {
                        case "":
                        case 0:
                        case "0":
                        case null:
                        case false:
                        case typeof this == "undefined":
                            return true;
                                default : return false;
                    }
                }

empty(null) // true
empty(0) // true
empty(7) // false
empty("") // true
empty((function() { return "" }) ) // true
Jet
+1  A: 

For checking if a string is empty, null or undefined I use:

function isEmpty(str) {
    return (!str || 0 === str.length);
}

For checking if a string is blank, null or undefined I use:

function isBlank(str) {
    return (!str || /^\s*$/.test(str));
}
janogonzalez
A: 

str.value.length == 0

Doug
A: 
function tell()
{
var pass = document.getElementById('pasword').value;
var plen = pass.length;

now you can check if your string is empty as like 
if(plen==0)
{
         alert('empty');
}
else
{
   alert('you entered something');
}
}


<input type='text' id='pasword' />

this is also a generic way to check if field is empty.

Muhammad Salman