开发者

Checking whether a variable is null or NOT

var DEST_VALUE = 1
var APPDAYS_AFTER = 2

}

How can i check whether the variable holds some value or开发者_如何学Python not. When i do this, it does not work...


Of course it doesn't do anything, because in your example DEST_VALUE resolves to true like APPDAYS_AFTER. Value that resolve to false when converted to a boolean in javascript are:

false
null
undefined
The empty string ''
The number 0
The number NaN (yep, 'Not a Number' is a number, it is a special number)

if you write

if(!DEST_VALUE){
    txtSiteId.value = fileContents.Settings.SiteID;
}

you write "if DEST_VALUE is not true do something" (in your case it does nothing). If you want to check if a variables hold a value:

if(DEST_VALUE !== undefined){
     //do something
}


I use such function to check if variable is empty or not:

function empty( mixed_var ) {
    return ( typeof(mixed_var) === 'undefined' || mixed_var === "" || mixed_var === 0   || mixed_var === "0" || mixed_var === null  || mixed_var === false );
}


I assume you mean "holds some value" like in "the variable has been created so it exists", right? Otherwise, your approach works perfectly fine.

If you want to check whether a variable exists in javascript, you have to check its parent object for the property - otherwise the script will fail. Each object in javascript belongs to a parent object, even if it seems to be global (then, it belongs to the window object). So, try something like:

  if (window.DEST_VALUE) 
    // do something
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜