Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Syntax question

Status
Not open for further replies.

vladibo

Programmer
Sep 14, 2003
161
0
0
CA
I've seen this syntax:

this.ie4 = !!(this.ie && !document.getElementById);

Can somebody explain to me why do I need !!, why not just:

this.ie4 = (this.ie && !document.getElementById);
 
the double-! is pointless, all it does is double-negate, which is the same as having no !'s in the first place. see this example:

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
	"[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]

<html>
<head>
<title>Untitled</title>
</head>

<body>

<script type="text/javascript"><!--

document.write( true + "<br />");
document.write( !true + "<br />");
document.write( !!true + "<br />");

//--></script>

</body>
</html>



*cLFlaVA
----------------------------
[tt]mr. pibb + red vines = crazy delicious![/tt]

[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
The double negative is not totally vain, just a little device to make type cast to boolean more visible/explicit. (I am not disagreeing with cLFlaVA though.) The pont there is something line appending a "" to a scalar to make string operation less prong to unexpected error in case the undefined/null turn up.
[tt]s=null; s=""+s;[/tt]
The same here for double negation.
[tt]
function x(y) {
alert ("[y]\t"+y+"\t"+typeof(y)+"\n"+"[!y]\t"+!y+"\t"+typeof(!y)+"\n"+"[!!y]\t"+!!y+"\t"+typeof(!!y))
}
x();
x("a");
x(true);
x(false);
x(1);
x(null);
x(new Array());
[/tt]
which make boolean type more certain.

Say for instance, a common occurrence of document.all for testing ie, we can make type casting more explicit and in a sense more comforting/ease of mind for the scripter by doing this.
[tt]if (!!document.all) {
// etc ...
}[/tt]
Just a little trick which might catch the eyes.
 
Thanks tsuji, that seem to be a reasonable argument.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top