admin管理员组文章数量:1394526
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 badges5 Answers
Reset to default 5JavaScript 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:
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.null
: this is an object value that can be assigned to anyvar
.
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.
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 */ }
you can test against the keyword
undefined
(note the triple-equals operator):if (undefined === myFoo) { /* this would be considered empty */ }
you can test against the keyword
null
(note the triple-equals operator):if (null === myFoo) { /* this would be considered empty */ }
you can test against the empty string
""
(note the triple-equals operator):if ("" === myFoo) { /* this would be the empty string */ }
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)
本文标签: Does VBScript39s IsEmpty have an equivalent in JavaScriptStack Overflow
版权声明:本文标题:Does VBScript's IsEmpty have an equivalent in JavaScript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744094945a2590113.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论