Peter Chamberlin

Navigation

Skip navigation.

Search

Site navigation

Email conversation

FromPeter Chamberlin
ToMe
SubjectCorrection
Date15 November 2007 14:15
Tarquin,
 
Re: http://www.howtocreate.co.uk/tutorials/javascript/controls
 
You show an expansion in the "Conditionals without a condition?" section of
positive and negated conditions to...

document.getElementById != "" &&
document.getElementById != 0 &&
document.getElementById != false &&
document.getElementById != undefined &&
document.getElementById != null

and:

document.getElementById == "" ||
document.getElementById == 0 ||
document.getElementById == false ||
document.getElementById == undefined ||
document.getElementById == null

...respectively. I think this should be corrected to use the strict type
equalities, since null == undefined == true. Thus:

document.getElementById !== "" &&
document.getElementById !== 0 &&
document.getElementById !== false &&
document.getElementById !== undefined &&
document.getElementById !== null

and:

document.getElementById === "" ||
document.getElementById === 0 ||
document.getElementById === false ||
document.getElementById === undefined ||
document.getElementById === null

Interestingly this shows that the negation condition should execute in
quicker average time :)

Regards,

Peter Chamberlin
FromMe
ToPeter Chamberlin
SubjectRe: Correction
Date18 November 2007 08:57
Peter,

> I think this should be corrected to use the strict type
> equalities, since null == undefined == true. Thus:

null can never equate to true. Only these are equivalent to each other:
0 == false == ''
and
undefined == null

The result would be the same using != or !==. It makes a few extra tests the
way I wrote it, but that does not effect the result. Internally, the browser
does not use that algorithm at all, it uses something very different to
achieve that result much faster. What I wrote was basically just to give you
an idea of what will gain a positive result and what will gain a negative
result. It is pseudo-code, not an actual example of what will be used.


Mark 'Tarquin' Wilton-Jones - author of http://www.howtocreate.co.uk/
FromPeter Chamberlin
ToMe
SubjectRe: Correction
Date19 November 2007 08:52
Mark,

Sorry, I meant "null == undefined is true". I pointed this out because
seeing two double equals equalities that were the same in the 'pseudo-code'
made me wonder if you were aware of that. I didn't consider it pseudo-code,
since it's runnable JavaScript. Like you say, I imagine the browser performs
a similar check when a variable's value is set in order to flag up certain
type implicit conversions.

Regards,

Peter Chamberlin
This site was created by Mark "Tarquin" Wilton-Jones.
Don't click this link unless you want to be banned from our site.