Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the javascript equivalant for vbscript `Is Nothing`

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) {  

}
like image 882
Madura Harshana Avatar asked Dec 07 '12 12:12

Madura Harshana


1 Answers

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.
}
like image 114
Nathan Wall Avatar answered Sep 19 '22 10:09

Nathan Wall