views:

935

answers:

5

Using Javascript, is there an equivalent function or functionality so VBScript's IsEmpty function? The VBScript function returns whether the given variable is "empty", e.g. since all the variables are actually VARIANTs, whether or not the VARIANT has a vt of VT_EMPTY, whereas most of what I've found for Javascript is string-specific or not generalizable.

+2  A: 
function IsEmpty( theValue ) 
{ 
    if ( theValue === undefined ) 
    { 
        return true; 
    } 
    return false; 
}
Robert Harvey
You need to use ===. as it is, IsEmpty(false) == true, which probably isn't what he wants (although it's been a long time since I've done any VBScript).
Matthew Crumley
Note this is not equivalent to VBScripts IsEmpty, which strictly returns true only when the variable holds Empty. VBScripts IsEmpty returns false if the value held is Null or "" or 0 or 30 Dec 1899. However Empty is equal to 0 and "" and 30 Dec 1899.
AnthonyWJones
A: 

Not there isnt.

You will have always to test your variables against "null", undefined and "". (that is boring)

Cleiton
A: 

function isEmpty(value){ return value == null || value === ""; }

kangax
+3  A: 

Not sure, if this is what it should be

typeof variable == "undefined"
shahkalpesh
+1 IMO the only correct answer here, nulls, "" etc are not empty values. Undefined is pretty much the equivalent of Empty in VBScript.
AnthonyWJones
+3  A: 

JavaScript has a number of different values which could be considered "empty" and a number of different ways to test for empty.

First things that are empty:

  1. undefined: values that have been not been assigned. if you create a variable but don't assign anything to it, this is what it will contain.

  2. null: this is an object value that can be assigned to any var.

Next how to test for them. JavaScript is loosely typed with a lot of implicit type conversion. This means that when two values are of different types, the standard equals operator == performs type coersion. The triple-equals operator does not perform type coersion which makes it the best operator for testing against very specific values.

  1. you can test the type of a value which returns a string that can be tested (note the triple-equals operator):

    if ("undefined" === typeof myFoo) { /* this would be considered empty */ }
    
  2. you can test against the keyword undefined (note the triple-equals operator):

    if (undefined === myFoo) { /* this would be considered empty */ }
    
  3. you can test against the keyword null (note the triple-equals operator):

    if (null === myFoo) { /* this would be considered empty */ }
    
  4. you can test against the empty string "" (note the triple-equals operator):

    if ("" === myFoo) { /* this would be the empty string */ }
    
  5. if you are expecting a value which would not normally be coerced to the boolean value false, then you can test for its existence by just testing it directly within the if statement:

    if (!myFoo) {
        /* this would be any falsy value, including false, null, 0, undefined */
    } else {
        /* this would be any truthy value, including objects,
        numbers other than zero, Dates, etc. */
    }
    

Most of the time, if you're expecting something "truthy", you can just test it directly and it will be coerced to a boolean value. So in short, you could probably get away with defining it as this:

function isEmpty(value) {
    return !value;
}

But it often makes more sense just to put a ! or !! before the object to get its value. Just be careful if you are expecting false or 0 (zero) as valid values. In those cases it is better to test the typeof the object and make choices accordingly.

McKAMEY
-1. Null is a value, so is "". Neither values would return True if passed to VBScripts IsEmpty function.
AnthonyWJones
In JavaScript that is rarely a useful definition of being empty.
McKAMEY