Velocity Reviews > typeof x == 'undefined' or x == undefined?

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

-Lost
Guest
Posts: n/a

 01-28-2007
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

Randy Webb
Guest
Posts: n/a

 01-28-2007
-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/

Richard Cornford
Guest
Posts: n/a

 01-28-2007
-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.

-Lost
Guest
Posts: n/a

 01-28-2007
"Randy Webb" <(E-Mail Removed)> wrote in message
news(E-Mail Removed)...
> -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/

"Richard Cornford" <(E-Mail Removed)> wrote in message
news:epj49b\$4q6\$1\$(E-Mail Removed)...
> -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.

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

Richard Cornford
Guest
Posts: n/a

 01-28-2007
-Lost wrote:
> Randy Webb wrote:

<snip>
>> --
>> Randy

<snip>

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

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

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
you explain what, why and where in your questions.

Richard.

-Lost
Guest
Posts: n/a

 01-28-2007
"Richard Cornford" <(E-Mail Removed)> wrote in message
news:epjb2a\$hul\$1\$(E-Mail Removed)...
> 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.

Gotcha. That was a mistake on my part. I meant to trim the excess, but forgot in haste.

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

OK, a couple things here.

1) VK has not replied to this post. (So... see #4.)

2) I am truly uninterested in your personal campaign against VK.

3) Please do not take the above as an assault on you. I value your feedback and have
thus far found it indespensible.

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.

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

In my current state of JavaScript-ness, I am woefully unaware of methods of providing
myself different contexts by which to test whatever. The what, why, and where has also,
thus far, been pointless. The reason being is because I get a simple idea and do random
little 10 to 30 line scripts just to see if I actually understand whatever it is I have

Be well.

-Lost

Richard Cornford
Guest
Posts: n/a

 01-29-2007
-Lost wrote:
> Richard Cornford wrote:

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

>
> OK, a couple things here.
>
> 1) VK has not replied to this post. (So... see #4.)

> 2) I am truly uninterested in your personal campaign against VK.

It is not personal. Anyone else who posted such a mass of utter nonsense
on such a regular basis, and over such an extended period, would be
subject to a similar response. In the past efforts have been made to
correct VK's misconceptions about javascript (and most other web
technologies), but mostly it takes an extreme effort to beat even the
most basic concepts into him and even then he tends not understand.
Having demonstrated his (seemingly wilful) inability to learn anything
about javascript the extent to which anyone is willing to attempt to
teach him has diminished, and instead he is mostly just criticised for
wasting everyone's time.

> 3) Please do not take the above as an assault on you. I
> value your feedback and have thus far found it indespensible.

It is a warning, you may take it as you like.

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

There is. The general rule is that if VK writes something the odds are
(based on past experience) better than 50-50 that it is false, fictional
(made up off the top of his head), misconceived, or the worst technique
available in any situation. However, very often what he is writing is so
detached from the reality of javascript, and so incoherent in itself,
that no specific correction can be made.

Remember that after what he claims is the best part of 10 years 'using'
javascript VK is incapable of assembling even 30 liens of code to carry

<URL:

-, so inept at testing that he cannot see for himself that the code he
writes does not work, and even when spoon-fed the functional code he
still creates an clumsy implementation. These are not symptoms that would
be expected from a javascript expert, though they might be manifest in a
mentally ill individual who suffered the delusion of being a javascript
expert.

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

Consider yourself informed, but do examine VK's posting record in the
archives. If you go back a year or two you may find many people trying to
beet some sort of understanding into him, and so read many simplified
explanations of important details of javascript presented in that effort.

>>> 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
>> conventions)
>> if you explain what, why and where in your questions.

>
> In my current state of JavaScript-ness, I am woefully unaware of
> methods
> of providing myself different contexts by which to test whatever.

I have no idea what you intend that to mean.

> The what, why, and where has also, thus far, been pointless.

You will not be able to write much software without knowing what you are
trying to do, or why you are trying to do it.

> The reason being is because I get a simple idea and do random little 10
> to 30 line scripts just to see if I actually understand
> whatever it is I have read.

That is a what and why (though it still lacks a where). Now in the
contest of testing for undefined values you must then be trying to see if
you actually understand something specific, but you did not mention what
it was that you thought you understood.

Richard.

Randy Webb
Guest
Posts: n/a

 01-29-2007
-Lost said the following on 1/28/2007 6:47 PM:

<snip>

> 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/

-Lost
Guest
Posts: n/a

 01-29-2007
"Randy Webb" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> -Lost said the following on 1/28/2007 6:47 PM:
>
> <snip>
>
>> 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.

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.

-Lost
Guest
Posts: n/a

 01-29-2007
"Richard Cornford" <(E-Mail Removed)> wrote in message
news:epji9c\$nic\$1\$(E-Mail Removed)...
> -Lost wrote:
>> Richard Cornford wrote:

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

>>
>> OK, a couple things here.
>>
>> 1) VK has not replied to this post. (So... see #4.)

>
>> 2) I am truly uninterested in your personal campaign against VK.

>
> It is not personal. Anyone else who posted such a mass of utter nonsense on such a
> regular basis, and over such an extended period, would be subject to a similar response.
> In the past efforts have been made to correct VK's misconceptions about javascript (and
> most other web technologies), but mostly it takes an extreme effort to beat even the
> most basic concepts into him and even then he tends not understand. Having demonstrated
> his (seemingly wilful) inability to learn anything about javascript the extent to which
> anyone is willing to attempt to teach him has diminished, and instead he is mostly just
> criticised for wasting everyone's time.

I am definitely seeing the trend.

>> 3) Please do not take the above as an assault on you. I
>> value your feedback and have thus far found it indespensible.

>
> It is a warning, you may take it as you like.
>
>> 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.

>
> There is. The general rule is that if VK writes something the odds are (based on past
> experience) better than 50-50 that it is false, fictional (made up off the top of his
> head), misconceived, or the worst technique available in any situation. However, very
> often what he is writing is so detached from the reality of javascript, and so
> incoherent in itself, that no specific correction can be made.
>
> Remember that after what he claims is the best part of 10 years 'using' javascript VK is
> incapable of assembling even 30 liens of code to carry out a relatively simple task:-
>
>
> -, so inept at testing that he cannot see for himself that the code he writes does not
> work, and even when spoon-fed the functional code he still creates an clumsy
> implementation. These are not symptoms that would be expected from a javascript expert,
> though they might be manifest in a mentally ill individual who suffered the delusion of
> being a javascript expert.
>
>> 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.

>
> Consider yourself informed, but do examine VK's posting record in the archives. If you
> go back a year or two you may find many people trying to beet some sort of understanding
> into him, and so read many simplified explanations of important details of javascript
> presented in that effort.

Definitely noted! I did a little perusal and found several humorous situations. Well,
perhaps not humorous but I chuckled nonetheless.

Thank you for the heads up.

>>>> 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
>>> if you explain what, why and where in your questions.

>>
>> In my current state of JavaScript-ness, I am woefully unaware of methods
>> of providing myself different contexts by which to test whatever.

>
> I have no idea what you intend that to mean.

Hehe, forgive me on that one. I swear it sounded so much more eloquent in my head. I was
basically trying to convey that because I am so new to JavaScript I may be limited as to
how to properly illustrate the what, why, and where (actually, the where is in a browser)
when I am just writing some little trivial thing to test.

Basically, aside from just posting a few lines that I wondered about, I really have no
purpose or context. I am just throwing a few things together to see how they work.

>> The what, why, and where has also, thus far, been pointless.

>
> You will not be able to write much software without knowing what you are trying to do,
> or why you are trying to do it.

Duly noted. When I begin writing more full-fledged applications that information will
definitely come in handy.

>> The reason being is because I get a simple idea and do random little 10 to 30 line
>> scripts just to see if I actually understand
>> whatever it is I have read.

>
> That is a what and why (though it still lacks a where). Now in the contest of testing
> for undefined values you must then be trying to see if you actually understand something
> specific, but you did not mention what it was that you thought you understood.

Actually, I thought it was implied. I thought that (I understood):

typeof x == 'undefined' was the same as x == undefined

....I of course have since been enlightened by both you and Randy. (Thanks again!)

Be well.

-Lost