473,790 Members | 2,850 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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
13 36901
-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.javas cript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jan 28 '07 #2
-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
"Randy Webb" <Hi************ @aol.comwrote in message
news:xd******** ************@te lcove.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.javas cript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
"Richard Cornford" <Ri*****@litote s.demon.co.ukwr ote in message
news:ep******** ***********@new s.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
-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
"Richard Cornford" <Ri*****@litote s.demon.co.ukwr ote in message
news:ep******** ***********@new s.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
-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.javas cript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jan 29 '07 #7
-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
"Randy Webb" <Hi************ @aol.comwrote in message
news:lL******** ************@te lcove.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
"Richard Cornford" <Ri*****@litote s.demon.co.ukwr ote in message
news:ep******** ***********@new s.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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
10019
by: Robert Mark Bram | last post by:
Hi All! I have the following code in an asp page whose language tag is: <%@LANGUAGE="JAVASCRIPT" CODEPAGE="65001"%> // Find request variables. var edition = Request.Form ("edition"); var language = Request.Form ("language"); Response.Write("Edition is type &quot;" + (typeof edition) + "&quot; and value &quot;" + edition + "&quot;<br>");
7
42576
by: Bennett Haselton | last post by:
Is there any way to find a string representing an object's class, which will work in Internet Explorer 6? "typeof" doesn't work -- it returns "object" for all objects: x = window.open('http://www.yahoo.com/'); alert(typeof x); And I found this page: http://www.mozilla.org/js/language/js20-2002-04/core/expressions.html
2
2089
by: Dag Sunde | last post by:
I have the following code fragment in one of my pages: if (typeof document.getElementById('myApplet').getTableAsSDV != 'undefined') { rowBuffer = document.getElementById('myApplet').getTableAsSDV(); } The code above have been working in IE, NS anf Firefox for a long time now, but have suddenly stopped working in IE 6, on WIN XP SP1.
9
10748
by: Klaus Johannes Rusch | last post by:
IE7 returns "unknown" instead of "undefined" when querying the type of an unknown property of an object, for example document.write(typeof window.missingproperty); Has "unknown" been defined as a valid return value for the typeof operator in a later version of ECMAScript or is this a JScript "feature"? -- Klaus Johannes Rusch
20
9318
by: effendi | last post by:
I am testting the following code in firefox function fDHTMLPopulateFields(displayValuesArray, displayOrderArray) { var i, currentElement, displayFieldID, currentChild, nDisplayValues = displayValuesArray.length; for (i=0; i<nDisplayValues; i++) {
11
4072
by: -Lost | last post by:
I have this generic function (JavaScript newbie here, so don't think I am going to impress you): function blah() { var container = ''; for(var i = 0; i < arguments.length; i++) { container += arguments + '\n'; }
3
2803
by: phocis | last post by:
I wrote a pair of functions to enable scoped or referenced setTimeout calls. I did this because I have an object factory that creates multiple objects and those objects need to delay a few calls on themselves at a certain point in time. This code works fine for normal objects. However, my issue occurs if at any point, an event object is passed across in IE. It seems, that events methods and variables in IE are PRIVATE (!) after the...
0
9512
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10145
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9986
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9021
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7530
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4094
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3707
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2909
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.