QuakeLive Posted November 14, 2013 Share Posted November 14, 2013 Hi,I have one question about undefined:Do these two lines do the same thing? 1. if (typeof something != 'undefined') 2. if (something !== undefined) Thank you in advance! Link to comment Share on other sites More sharing options...
HartleySan Posted November 14, 2013 Share Posted November 14, 2013 Hello and welcome to the forums, QuakeLive. To answer your question, yes and no. Yes, they will normally produce the same result, but typeof is generally preferred because undefined is mutable, as explained by the following: http://wtfjs.com/2010/02/15/undefined-is-mutable http://stackoverflow.com/questions/4725603/variable-undefined-vs-typeof-variable-undefined Please also note that you should almost always use !== instead of !=. 1 Link to comment Share on other sites More sharing options...
QuakeLive Posted November 15, 2013 Author Share Posted November 15, 2013 Thank you HartleySan! Although I read this book, I do not think I understood some things well. For example: You can change the value of undefined: undefined = 42; So, now undefined is number? And, if I understood well - it is better to use everywhere (typeof something != 'undefined'), for example: if (typeof output.textContent != 'undefined') { output.textContent = message; } else { output.innerText = message; } (than if (output.textContent !== undefined) { output.textContent = message; } else { output.innerText = message; } as used in the book?) Link to comment Share on other sites More sharing options...
HartleySan Posted November 15, 2013 Share Posted November 15, 2013 Yes, if you were to do undefined = 42;, then undefined would now be equal to the number 42. The important thing to note is that (for whatever reason), undefined is treated like any other global variable in JavaScript, and as such, values can be assigned to it. For that reason, doing the typeof comparison is generally safer. With that said, if you have control of all your code, and you're not worried about somebody trying to mess with your script by reassigning a value to undefined, then you don't have to worry about that, and either comparison is fine. Like I said, in most cases, the comparisons are the same, but it's worth noting the caveat with undefined being mutable. Also, like I mentioned before, please note the difference between != and !==, and try to always use !==. Thanks. 2 Link to comment Share on other sites More sharing options...
QuakeLive Posted November 18, 2013 Author Share Posted November 18, 2013 Thank you HartleySan. Yes, if you were to do undefined = 42;, then undefined would now be equal to the number 42. This is what I didn't know... The problem is that when I test this - undefined is not equal to the number 42? (or I make a mistake somewhere? ) : Link to comment Share on other sites More sharing options...
HartleySan Posted November 18, 2013 Share Posted November 18, 2013 Hmmm... you're right. That's interesting. My guess would be that browser makers realized the danger of allowing undefined to be redefined, and block any attempts to do so. If that's the case, then with newer browsers, you no longer have to worry about this issue (although it'll still be an issue in older browsers (i.e., old IE)). 1 Link to comment Share on other sites More sharing options...
QuakeLive Posted November 18, 2013 Author Share Posted November 18, 2013 Hmmm... you're right. That's interesting. My guess would be that browser makers realized the danger of allowing undefined to be redefined, and block any attempts to do so. If that's the case, then with newer browsers, you no longer have to worry about this issue (although it'll still be an issue in older browsers (i.e., old IE)). I found this... where someone wrote that "The ES5 standard dictates that undefined is now readonly." 1 Link to comment Share on other sites More sharing options...
HartleySan Posted November 18, 2013 Share Posted November 18, 2013 Very good find, QuakeLive. That's actually what I was thinking, but didn't take the time to research it myself. Thanks. Link to comment Share on other sites More sharing options...
Recommended Posts