By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
460,023 Members | 1,297 Online
Bytes IT Community
+ Ask a Question
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
Share this Question
Share on Google+
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" <Hi************@aol.comwrote in message
news:xd********************@telcove.net...
-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" <Ri*****@litotes.demon.co.ukwrote in message
news:ep*******************@news.demon.co.uk...
-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
Jan 28 '07 #4

P: n/a
-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
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, 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
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
"Richard Cornford" <Ri*****@litotes.demon.co.ukwrote in message
news:ep*******************@news.demon.co.uk...
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
read.

Be well.

-Lost
Jan 28 '07 #6

P: n/a
-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/
Jan 29 '07 #7

P: n/a
-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
out a relatively simple task:-

<URL:
http://groups.google.co.uk/group/com...20fbcd4b4ab7f8 >

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

Jan 29 '07 #8

P: n/a
"Randy Webb" <Hi************@aol.comwrote in message
news:lL********************@telcove.net...
-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.
Jan 29 '07 #9

P: n/a
"Richard Cornford" <Ri*****@litotes.demon.co.ukwrote in message
news:ep*******************@news.demon.co.uk...
-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:-

<URL: http://groups.google.co.uk/group/com...20fbcd4b4ab7f8 >

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

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
Jan 29 '07 #10

P: n/a
VK
Some people are so easy going to propaganda... :-\ :-)
Let's see the back effect...

Randy Webb and Richard Cornford are two clueless trolls on clj who
greatly compensate the lack of knowledge by laud talking and hasty
insults. The best you can do for your learning curbe is to disregard
everything they have to say, trust me. A standard killfile filter for
these two names will greatly improve your clj experience.

P.S. Now I would like to see what exactly of my posts mislead or
misinformed you?
"I had noticed this, but did not feel that I had adequate evidence to
make that assumption."
So "adequate evidence" of me misleading and misinforming you was the
abstract statement "Ignore VK"?

Yes I know, it is a hard job to be a pleaser between two sides. I have
a great advise for the future: simply disregard any posts - by VK,
Randy Webb, Richard Cornford and anyone else - containing abstract
statements "don't trust him, trust me" and similar.

P.P.S. Randy Webb and Richard Cornford: "ignore VK" will be prosecuted
to the full extend of English language. For the time being better
concentrate on the rounding FAQ - with the recent "splendid"
Cornford's comments it is a real manifestation of a deep ignorance
pretending to be a knowledge.

Jan 29 '07 #11

P: n/a
VK
On Jan 29, 1:52 pm, "VK" <schools_r...@yahoo.comwrote:
<snip>

-Lost, come out... No shooting I promise - see, my hands are empty. No
shooting.

:-))

fgvpxvat gb gur evtug zna vf abg n jbzna-bayl uneq pubvpr ceboyrz :-)

Trust to Crockford, VK or (best of all) to no one. Ask any questions,
accept any opinions - it is your holly right.

Jan 29 '07 #12

P: n/a
VK said the following on 1/29/2007 5:52 AM:
Some people are so easy going to propaganda... :-\ :-)
You make it trivially simple with your postings.
Let's see the back effect...

Randy Webb and Richard Cornford are two clueless trolls on clj who
greatly compensate the lack of knowledge by laud talking and hasty
insults. The best you can do for your learning curbe is to disregard
everything they have to say, trust me. A standard killfile filter for
these two names will greatly improve your clj experience.
That paragraph is easy for you to prove and it is equally as easy for
you to disprove my beliefs about you. Point to a thread in the last year
that I or Richard have posted the type of nonsense code you post and
then when told it was garbage code either of us responded with "I don't
care about that scenario".

Until you can do that (and you can't) your opinion is - as always -
complete rubbish coming from the mind of a half wit.

<snip>
P.P.S. Randy Webb and Richard Cornford: "ignore VK" will be prosecuted
to the full extend of English language.
Please prosecute me all you want, just base your prosecution on truth
and URL's to back up your accusations. It is a trivial thing to do with
you. It only takes looking at the rounding thread and the garbage code
you posted (and your replies with it) to see that the condemnation of
your ability (using that word loosely) is very well deserved.

Your turn halfwit.
For the time being better concentrate on the rounding FAQ
There is nothing for me to concentrate on as the only person who has
displayed an inability to read and understand what is going on is you.

I would tell you that in some states in the US suicide is legal for you
but you would - inevitably - f**k that up too.

--
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 30 '07 #13

P: n/a
VK wrote:
<snip>
P.P.S. Randy Webb and Richard Cornford: "ignore VK" will be
prosecuted to the full extend of English language.
<snip>

Aren't you forgetting the evidence? Where do you think you would find an
expert witness who will not either end up agreeing with my assessment of
your posts, or be exposed as not so much of an expert?

On the other hand, I don't recall any law that will allow you to be
anonymous and also to complain about anything anyone says about your
avatar.

Richard.

Jan 31 '07 #14

This discussion thread is closed

Replies have been disabled for this discussion.