460,023 Members | 1,297 Online
Need help? Post your question and get tips & solutions from a community of 460,023 IT Pros & Developers. It's quick & easy.

# typeof x == 'undefined' or x == undefined?

 P: n/a You can test for an undefined value in two ways: function blah(x) { if (x == undefined) { x = 'default value'; } } That could also have been written: function blah(x) { if (typeof x == 'undefined') { x = 'default value'; } } Is either one of these more "correct" than the other? Is there an instance where one might fail over the other? -Lost Jan 28 '07 #1
13 Replies

 P: n/a -Lost said the following on 1/28/2007 3:59 PM: You can test for an undefined value in two ways: function blah(x) { if (x == undefined) { x = 'default value'; } } That could also have been written: No, as it is testing two different things. function blah(x) { if (typeof x == 'undefined') { x = 'default value'; } } Is either one of these more "correct" than the other? Is there an instance where one might fail over the other? It depends - directly - on what you are trying to test for. if(x == undefined) is testing the value if (typeof x = undefined) is testing the existence of x -- Randy Chance Favors The Prepared Mind comp.lang.javascript FAQ - http://jibbering.com/faq/index.html Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/ Jan 28 '07 #2

 P: n/a -Lost wrote: You can test for an undefined value in two ways: function blah(x) { if (x == undefined) { x = 'default value'; } } Using the type-converting equality operator has - null - equalling - undefined -. That could also have been written: function blah(x) { if (typeof x == 'undefined') { x = 'default value'; } } Is either one of these more "correct" than the other? The "correct" test is determined by what you want to know, and the context in which you want to know it. Is there an instance where one might fail over the other? They are different tests, so they answer different questions. Both would "fail" if applied in the wrong situation. Richard. Jan 28 '07 #3

 P: n/a "Randy Webb" You can test for an undefined value in two ways:function blah(x){if (x == undefined) { x = 'default value'; }}That could also have been written: No, as it is testing two different things. >function blah(x){if (typeof x == 'undefined') { x = 'default value'; }}Is either one of these more "correct" than the other? Is there an instance where onemight fail over the other? It depends - directly - on what you are trying to test for. if(x == undefined) is testing the value if (typeof x = undefined) is testing the existence of x -- Randy Chance Favors The Prepared Mind comp.lang.javascript FAQ - http://jibbering.com/faq/index.html Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/ "Richard Cornford" You can test for an undefined value in two ways:function blah(x){if (x == undefined) { x = 'default value'; }} Using the type-converting equality operator has - null - equalling - undefined -. >That could also have been written:function blah(x){if (typeof x == 'undefined') { x = 'default value'; }}Is either one of these more "correct" than the other? The "correct" test is determined by what you want to know, and the context in which you want to know it. >Is there an instance where one might fail over the other? They are different tests, so they answer different questions. Both would "fail" if applied in the wrong situation. Richard. OK, I see now. This self-taught crash course is causing me to amass ridiculous assumptions. I definitely should have tested it further, perhaps then I would have caught/understood that. Thanks, guys. -Lost Jan 28 '07 #4

 P: n/a -Lost wrote: Randy Webb wrote: >--Randy You should never be quoting signatures on Usenet (unless their contents are the direct subject of comment in the response). You should probably take the time to familiarise yourself with Usenet posting conventions (the group's FAQ is a reasonable place to start) as if you do not take them seriously you may find yourself not being take seriously by the people you want answers from. Richard Cornford wrote: >-Lost wrote: >>You can test for an undefined value in two ways:function blah(x){if (x == undefined) { x = 'default value'; }} Using the type-converting equality operator has - null - equalling- undefined -. >>That could also have been written:function blah(x){if (typeof x == 'undefined') { x = 'default value'; }}Is either one of these more "correct" than the other? The "correct" test is determined by what you want to know, andthe context in which you want to know it. >>Is there an instance where one might fail over the other? They are different tests, so they answer different questions. Bothwould "fail" if applied in the wrong situation.Richard. OK, I see now. This self-taught crash course is causing me to amass ridiculous assumptions. Not nearly as quickly as taking VK seriously will. I definitely should have tested it further, perhaps then I would have caught/understood that. In general the 'correct' way of doing anything depends considerably on the context in which you want to do it. You will get better answers to your questions (subject to your not disregarding posting conventions) if you explain what, why and where in your questions. Richard. Jan 28 '07 #5

 P: n/a -Lost said the following on 1/28/2007 6:47 PM: 4) If there is a response made by VK to one of my original inquiries that is somehow flawed or generally in err, *please* let me know. I have just begun reading articles and sadly a somewhat antiquated book on JavaScript. If VK is feeding me erroneous information you would be doing me a great favor by making me aware of it. Ignore *everything* VK has to say and you will *never* go wrong. Listening to anything VK has to say will eventually bite you in places you don't want to be bitten. That is not an anti-VK attitude, it is an attitude/opinion based on past postings of VK and the inability of VK to understand what is said to him/her. -- Randy Chance Favors The Prepared Mind comp.lang.javascript FAQ - http://jibbering.com/faq/index.html Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/ Jan 29 '07 #7

 P: n/a "Randy Webb" >4) If there is a response made by VK to one of my original inquiries that is somehowflawed or generally in err, *please* let me know. I have just begun reading articlesand sadly a somewhat antiquated book on JavaScript. If VK is feeding me erroneousinformation you would be doing me a great favor by making me aware of it. Ignore *everything* VK has to say and you will *never* go wrong. Listening to anything VK has to say will eventually bite you in places you don't want to be bitten. That is not an anti-VK attitude, it is an attitude/opinion based on past postings of VK and the inability of VK to understand what is said to him/her. Duly noted! Thank you. I had noticed this, but did not feel that I had adequate evidence to make that assumption. Everything has been made perfectly clear though. Be well. -Lost P.S. Richard, thanks for the initial heads up. Accept my apologies if I offended you. Jan 29 '07 #9