Condition Always true when checking for "undefined" in Javascript
-
Good Day all i am reluctantly maintaining a Classic Asp application that will be re-written in few months to come. i have an issue where i have a Code like this
if(document.frmEntry.optMethod != "undefined") { if (document.frmEntry.optMethod.selectedIndex == 0) { strPrompt=strPrompt+'Please specify the Valuation Method\\n'; if ( objFocus == null ) { objFocus = document.frmEntry.optMethod; } } }
my problem here is that even if document.frmEntry.optMethod is not equal to "undefined" the code still go into the condition as if it was true. i have attached a proof in my debuger. http://www.vetauinvest.com/Example/IE_DEBUGGER.png[^] Thanks
Vuyiswa Maseko, Spoted in Daniweb-- Sorry to rant. I hate websites. They are just wierd. They don't behave like normal code. C#/VB.NET/ASP.NET/SQL7/2000/2005/2008 http://www.vuyiswamaseko.com vuyiswa[at]dotnetfunda.com http://www.Dotnetfunda.com
That should be
undefined
and NOT"undefined"
!!!I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)
-
Good Day all i am reluctantly maintaining a Classic Asp application that will be re-written in few months to come. i have an issue where i have a Code like this
if(document.frmEntry.optMethod != "undefined") { if (document.frmEntry.optMethod.selectedIndex == 0) { strPrompt=strPrompt+'Please specify the Valuation Method\\n'; if ( objFocus == null ) { objFocus = document.frmEntry.optMethod; } } }
my problem here is that even if document.frmEntry.optMethod is not equal to "undefined" the code still go into the condition as if it was true. i have attached a proof in my debuger. http://www.vetauinvest.com/Example/IE_DEBUGGER.png[^] Thanks
Vuyiswa Maseko, Spoted in Daniweb-- Sorry to rant. I hate websites. They are just wierd. They don't behave like normal code. C#/VB.NET/ASP.NET/SQL7/2000/2005/2008 http://www.vuyiswamaseko.com vuyiswa[at]dotnetfunda.com http://www.Dotnetfunda.com
The "undefined" shown in the debugger is not a string, so you can't compare it as one. To really test if your value is undefined, you can use this:
if(typeof document.frmEntry.optMethod != 'undefined')
The
typeof
operator returns the type of the argument as a string. -
The "undefined" shown in the debugger is not a string, so you can't compare it as one. To really test if your value is undefined, you can use this:
if(typeof document.frmEntry.optMethod != 'undefined')
The
typeof
operator returns the type of the argument as a string.I can't see why use
undefined
as string - with or without typeof!? In JavaScriptundefined
is a value property of the global namespace and yous should use it according to it...I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)
-
Try using a Falsy check. The following values are always falsy: false 0 (zero) "" (empty string) null undefined NaN (a special Number value meaning Not-a-Number!) Your code should look like as follows:
if(!document.frmEntry.optMethod)
{
if (document.frmEntry.optMethod.selectedIndex == 0) {
strPrompt=strPrompt+'Please specify the Valuation Method\n';
if ( objFocus == null ) {
objFocus = document.frmEntry.optMethod;
}
}
}You are very true about 'false' check, but sometimes it is a good practice to write full code and explicitly state against what are you want to check. It's of course only a matter of maintainability/readability...
I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)
-
I can't see why use
undefined
as string - with or without typeof!? In JavaScriptundefined
is a value property of the global namespace and yous should use it according to it...I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)
Thank you , this info is enough to solve the issue. thanks again
Vuyiswa Maseko, Spoted in Daniweb-- Sorry to rant. I hate websites. They are just wierd. They don't behave like normal code. C#/VB.NET/ASP.NET/SQL7/2000/2005/2008 http://www.vuyiswamaseko.com vuyiswa[at]dotnetfunda.com http://www.Dotnetfunda.com
-
I can't see why use
undefined
as string - with or without typeof!? In JavaScriptundefined
is a value property of the global namespace and yous should use it according to it...I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)
The typeof for an undefined value is always "undefined". The value undefined being in the global namespace opens up some possible problems:
function madness() {
// override with local variable
var a = 'fish', undefined = 'fish';if(a === undefined) {
// both 'fish', so here we are
}
}function mistake() {
var a = 'fish';// common mistake, using '=' instead of '=='
if(undefined = a) {
// 'undefined' is not a constant or keyword, so the whole
// expression is valid and will be true
}
}I know these are pretty unlikely scenarios, but better safe than sorry. In earlier browsers "undefined" was not a testable value so expressions like
(a == undefined)
would produce the helpful message "undefined is undefined". -
Good Day all i am reluctantly maintaining a Classic Asp application that will be re-written in few months to come. i have an issue where i have a Code like this
if(document.frmEntry.optMethod != "undefined") { if (document.frmEntry.optMethod.selectedIndex == 0) { strPrompt=strPrompt+'Please specify the Valuation Method\\n'; if ( objFocus == null ) { objFocus = document.frmEntry.optMethod; } } }
my problem here is that even if document.frmEntry.optMethod is not equal to "undefined" the code still go into the condition as if it was true. i have attached a proof in my debuger. http://www.vetauinvest.com/Example/IE_DEBUGGER.png[^] Thanks
Vuyiswa Maseko, Spoted in Daniweb-- Sorry to rant. I hate websites. They are just wierd. They don't behave like normal code. C#/VB.NET/ASP.NET/SQL7/2000/2005/2008 http://www.vuyiswamaseko.com vuyiswa[at]dotnetfunda.com http://www.Dotnetfunda.com
-
The typeof for an undefined value is always "undefined". The value undefined being in the global namespace opens up some possible problems:
function madness() {
// override with local variable
var a = 'fish', undefined = 'fish';if(a === undefined) {
// both 'fish', so here we are
}
}function mistake() {
var a = 'fish';// common mistake, using '=' instead of '=='
if(undefined = a) {
// 'undefined' is not a constant or keyword, so the whole
// expression is valid and will be true
}
}I know these are pretty unlikely scenarios, but better safe than sorry. In earlier browsers "undefined" was not a testable value so expressions like
(a == undefined)
would produce the helpful message "undefined is undefined".Graham Breach wrote:
The typeof for an undefined value is always "undefined".
How that?
var undefined = 'fish';
alert(typeof undefined); // string!!!I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)
-
Graham Breach wrote:
The typeof for an undefined value is always "undefined".
How that?
var undefined = 'fish';
alert(typeof undefined); // string!!!I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)
Yes. If you define
undefined
as a string variable, then it is no longer undefined and things get confusing. This is why I was testing the type of the value against the string "undefined" - the string is a constant. -
Good Day all i am reluctantly maintaining a Classic Asp application that will be re-written in few months to come. i have an issue where i have a Code like this
if(document.frmEntry.optMethod != "undefined") { if (document.frmEntry.optMethod.selectedIndex == 0) { strPrompt=strPrompt+'Please specify the Valuation Method\\n'; if ( objFocus == null ) { objFocus = document.frmEntry.optMethod; } } }
my problem here is that even if document.frmEntry.optMethod is not equal to "undefined" the code still go into the condition as if it was true. i have attached a proof in my debuger. http://www.vetauinvest.com/Example/IE_DEBUGGER.png[^] Thanks
Vuyiswa Maseko, Spoted in Daniweb-- Sorry to rant. I hate websites. They are just wierd. They don't behave like normal code. C#/VB.NET/ASP.NET/SQL7/2000/2005/2008 http://www.vuyiswamaseko.com vuyiswa[at]dotnetfunda.com http://www.Dotnetfunda.com
You can try with remove ("") double quotes OR use ('') single quotes.