最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Does VBScript's IsEmpty have an equivalent in JavaScript? - Stack Overflow

programmeradmin10浏览0评论

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.

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.

Share Improve this question asked Aug 14, 2009 at 3:53 dlanoddlanod 9,0309 gold badges59 silver badges98 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 5

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.

Not sure, if this is what it should be

typeof variable == "undefined"
function IsEmpty( theValue ) 
{ 
    if ( theValue === undefined ) 
    { 
        return true; 
    } 
    return false; 
}

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

Not there isnt.

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

发布评论

评论列表(0)

  1. 暂无评论