Test for null or undefined?

A

Alan Gutierrez

I'm wondering how I check for a missing argument. I'm creating a
jQuery extension, so maybe there is a best practice specific to
jQuery.


function foo( data, type ) {
if ( type == null ) type = "text"
bar( data, type )
}

Should I be testing for null or undefined?

Alan Gutierrez
 
S

Stevo

Alan said:
I'm wondering how I check for a missing argument. I'm creating a
jQuery extension, so maybe there is a best practice specific to
jQuery.


function foo( data, type ) {
if ( type == null ) type = "text"
bar( data, type )
}

Should I be testing for null or undefined?

Alan Gutierrez

I wouldn't use the name type, but assuming the name type is OK, then
if(type==undefined) will work. You could also check arguments.length
 
T

Thomas Allen

I wouldn't use the name type, but assuming the name type is OK, then
if(type==undefined) will work. You could also check arguments.length

I'd go with checking Arguments.length.

Thomas
 
J

Jorge

I'm wondering how I check for a missing argument. (...)

function foo( data, type ) {
    if ( type == null ) type = "text" (...)

Should I be testing for null or undefined?

None. Just write: type= type || "text";

If type is missing or "falsy" (null, undefined, 0, false or "") it
will become "text".
Using arguments slows down execution and will throw an error (IIRC) in
ECMAScript 5 "strict".
 
S

Stevo

Jorge said:
None. Just write: type= type || "text";

If type is missing or "falsy" (null, undefined, 0, false or "") it
will become "text".
Using arguments slows down execution and will throw an error (IIRC) in
ECMAScript 5 "strict".

I was going to suggest that, but he didn't want falsy, just undefined.
For example 0 or "" might be acceptable inputs.
 
G

Gregor Kofler

Am Mon, 30 Mar 2009 20:59:35 +0200 schrieb Stevo:
I wouldn't use the name type, but assuming the name type is OK, then
if(type==undefined) will work. You could also check arguments.length

If explicitly checking for undefined I'd recommend using

typeof yourVar == "undefined"

since undefined can be set to any value and type == undefined can give
"unexpected" results. In the OPs example

if(!type) {type = "text"} is perhaps sufficient.

Gregor
 
J

Jorge

I was going to suggest that, but he didn't want falsy, just undefined.
For example 0 or "" might be acceptable inputs.

Looks like if "type" was a string... but who knows.
 
D

David Mark

and `NaN`


That's not true.

First of all, there's no ECMAScript 5 (to my knowledge).

Opinion appears divided on the subject. "Jorge" says there is,
everyone else says there isn't. Given his recent tightrope walk with
lucidity, his claim should be dismissed out of hand.

[snip]
 
T

Thomas Allen

and `NaN`
That's not true.
First of all, there's no ECMAScript 5 (to my knowledge).

Opinion appears divided on the subject.  "Jorge" says there is,
everyone else says there isn't.  Given his recent tightrope walk with
lucidity, his claim should be dismissed out of hand.

[snip]

Give the guy a break. He already admitted that he was incorrect:
Forget that nonsense ^^^^^^. Sorry :)

Thomas
 
D

David Mark

Opinion appears divided on the subject.  "Jorge" says there is,
everyone else says there isn't.  Given his recent tightrope walk with
lucidity, his claim should be dismissed out of hand.

Give the guy a break. He already admitted that he was incorrect:
Forget that nonsense ^^^^^^. Sorry :)

I didn't write that you pugnacious little pissant. Learn to quote or
go back to jQuery-land. Sheesh.

Looking back at the previous posts, "Jorge" wrote the above line and
it did *not* refer to his delusions about "ECMAScript 5."
 
G

Garrett Smith

Alan said:
I'm wondering how I check for a missing argument.

Check arguments.length;
I'm creating a
jQuery extension, so maybe there is a best practice specific to
jQuery.


function foo( data, type ) {
if ( type == null ) type = "text"
bar( data, type )
}

Should I be testing for null or undefined?

You could test for either, but == isn't going to differentiate between
the two.

null == undefined

- was posted within the last two days.
Alan Gutierrez

Garrett
 
A

Alan Gutierrez

I was going to suggest that, but he didn't want falsy, just undefined.
For example 0 or "" might be acceptable inputs.

Actually, I figured out that I'm testing for a specific value for
type, so I don't need to worry about the absence of the value, I don't
think.

function foo( data, kindOfData ) {
if ( kindOfData == "xml" ) {
// If the data kind is xml, baz the data.
// If the data kind is null or undefined, this will be skipped.
data = baz( data );
}
bar( data, kindOfData );
}

So, Jorge's example made me rethink. Thanks for everyone's help on
varargs.

Alan Gutierrez
 
T

Thomas Allen

Jorge wrote:
I'm wondering how I check for a missing argument. (...)
function foo( data, type ) {
    if ( type == null ) type = "text" (...)
Should I be testing for null or undefined?
None. Just write: type= type || "text";
If type is missing or "falsy" (null, undefined, 0, false or "") it
and `NaN`
will become "text".
Using arguments slows down execution and will throw an error (IIRC) in
ECMAScript 5 "strict".
That's not true.
First of all, there's no ECMAScript 5 (to my knowledge).
Opinion appears divided on the subject.  "Jorge" says there is,
everyone else says there isn't.  Given his recent tightrope walk with
lucidity, his claim should be dismissed out of hand.
[snip]
Give the guy a break. He already admitted that he was incorrect:

I didn't write that you pugnacious little pissant.  Learn to quote or
go back to jQuery-land.  Sheesh.

Looking back at the previous posts, "Jorge" wrote the above line and
it did *not* refer to his delusions about "ECMAScript 5."

Hi David,

You seem to be a very negative, mean-spirited individual. I sincerely
hope that things look up for you, at the very least so that you don't
drive away more well-intentioned individuals with imperfect knowledge.
Enjoy your mailing list. But it would be irresponsible of me to not
report this to the list's owner as abuse, so I will do so.

Thomas
 
D

David Mark

Jorge wrote:
I'm wondering how I check for a missing argument. (...)
function foo( data, type ) {
    if ( type == null ) type = "text" (...)
Should I be testing for null or undefined?
None. Just write: type= type || "text";
If type is missing or "falsy" (null, undefined, 0, false or "")it
and `NaN`
will become "text".
Using arguments slows down execution and will throw an error (IIRC) in
ECMAScript 5 "strict".
That's not true.
First of all, there's no ECMAScript 5 (to my knowledge).
Opinion appears divided on the subject.  "Jorge" says there is,
everyone else says there isn't.  Given his recent tightrope walk with
lucidity, his claim should be dismissed out of hand.
[snip]
Give the guy a break. He already admitted that he was incorrect:
Forget that nonsense ^^^^^^. Sorry :)
I didn't write that you pugnacious little pissant.  Learn to quote or
go back to jQuery-land.  Sheesh.
Looking back at the previous posts, "Jorge" wrote the above line and
it did *not* refer to his delusions about "ECMAScript 5."

Hi David,

Hi. How are you?
You seem to be a very negative, mean-spirited individual. I sincerely

I am truly sorry you feel that way.
hope that things look up for you, at the very least so that you don't
drive away more well-intentioned individuals with imperfect knowledge.

Cue the violins.
Enjoy your mailing list.

Glad you feel that way, despite the misconceptions.
But it would be irresponsible of me to not
report this to the list's owner as abuse, so I will do so.

So you are going to report me to myself?

As with your destructive jQuery habit, I will try to save you some
time here. It's not a list, I don't own it and neither does anyone
else.
 
T

Tim Streater

Thomas Allen said:
You seem to be a very negative, mean-spirited individual. I sincerely
hope that things look up for you, at the very least so that you don't
drive away more well-intentioned individuals with imperfect knowledge.
Enjoy your mailing list. But it would be irresponsible of me to not
report this to the list's owner as abuse, so I will do so.

Which mailing list are you talking about?
 
T

Thomas Allen


Just re-joined for one last message because, frankly, this is
priceless:
http://www.reddit.com/r/programming/comments/7vul9/some_technical_criticism_of_jquery_code/

Apparently I'm not alone. I had a twilight zone moment there my code
seemed like utter shite because I was taking David Mark seriously.
Good to know he's just a pretentious turd with limited credentials :^)

Anyway, good luck DUDE (since it seems that ticks you off).

Thomas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top