Jump to content
Larry Ullman's Book Forums

Recommended Posts

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 !=.

  • Upvote 1
Link to comment
Share on other sites

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

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.

  • Upvote 2
Link to comment
Share on other sites

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? :huh: ) :

 

ezrjow.png

Link to comment
Share on other sites

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

  • Upvote 1
Link to comment
Share on other sites

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." :)

  • Upvote 1
Link to comment
Share on other sites

 Share

×
×
  • Create New...