Jump to content
Larry Ullman's Book Forums

Recommended Posts

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! :)

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
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?)

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...