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

What is the javascript equivalant for vbscript `Is Nothing` - Stack Overflow

programmeradmin6浏览0评论
If Not (oResponse.selectSingleNode("BigGroupType") Is Nothing) Then

End If

I need to convert this to javascript. Is that enough to check null?

This was my lead's answer, plz verify this,

if(typeof $(data).find("BigGroupType").text() !=  "undefined" && $(data).find("BigGroupType").text() != null) {  

}
If Not (oResponse.selectSingleNode("BigGroupType") Is Nothing) Then

End If

I need to convert this to javascript. Is that enough to check null?

This was my lead's answer, plz verify this,

if(typeof $(data).find("BigGroupType").text() !=  "undefined" && $(data).find("BigGroupType").text() != null) {  

}
Share Improve this question edited Dec 26, 2013 at 14:38 Kevin Fegan 1,2901 gold badge17 silver badges29 bronze badges asked Dec 7, 2012 at 12:53 Madura HarshanaMadura Harshana 1,2998 gold badges25 silver badges40 bronze badges
Add a comment  | 

5 Answers 5

Reset to default 13

JavaScript has two values which mean "nothing", undefined and null. undefined has a much stronger "nothing" meaning than null because it is the default value of every variable. No variable can be null unless it is set to null, but variables are undefined by default.

var x;
console.log(x === undefined); // => true

var X = { foo: 'bar' };
console.log(X.baz); // => undefined

If you want to check to see if something is undefined, you should use === because == isn't good enough to distinguish it from null.

var x = null;
console.log(x == undefined); // => true
console.log(x === undefined); // => false

However, this can be useful because sometimes you want to know if something is undefined or null, so you can just do if (value == null) to test if it is either.

Finally, if you want to test whether a variable even exists in scope, you can use typeof. This can be helpful when testing for built-ins which may not exist in older browsers, such as JSON.

if (typeof JSON == 'undefined') {
    // Either no variable named JSON exists, or it exists and
    // its value is undefined.
}

You need to check for both null and undefined, this implicitly does so

if( oResponse.selectSingleNode("BigGroupType") != null ) {

}

It is the equivalent of:

var node = oResponse.selectSingleNode("BigGroupType");
if( node !== null &&
    node !== void 0 ) {

}

void 0 being a bulletproof expression to get undefined

In JavaScript equvalent for Nothing is undefined

if(oResponse.selectSingleNode("BigGroupType") != undefined){

}

This logic:

If Not (oResponse.selectSingleNode("BigGroupType") Is Nothing)

Can be written like this in JavaScript:

if (typeof oResponse.selectSingleNode("BigGroupType") != 'undefined')

Nothing would equal undefined, but checking against undefined is not recommended for several reasons, it’s generally safer to use typeof.

However, if the selectSingleNode can return other falsy values such as null, it’s probably OK to just do a simple check if it is truthy:

if (oResponse.selectSingleNode("BigGroupType"))

JavaScript:-

(document.getElementById(“BigGroupType”) == undefined) // Returns true

JQuery:-

($(“#BigGroupType”).val() === “undefined”) // Returns true

Note in above examples undefined is a keyword in JavaScript, where as in JQuery it is just a string.

发布评论

评论列表(0)

  1. 暂无评论