/    Sign up×
Community /Pin to ProfileBookmark

What does ‘!!’ mean/is used for?

Hi

I’m reading a book about HTML5 and I have found the following expression:

function supports_canvas() {
return !!document.createElement(‘canvas’).getContext;
}

Is ‘!!’ a double negation? In this case, why is that used? Is it not the same that

function supports_canvas() {
return document.createElement(‘canvas’).getContext;
}

I have seen this ‘!!’ more and more in JS code.

Thanks a lot

Isi

to post a comment
JavaScript

10 Comments(s)

Copy linkTweet thisAlerts:
@KorMay 16.2011 — The javascript double negative !! is used to turn any data type (null, undefined, objects) to a Boolean.

The coder wanted, in your case, to make sure that the returned value is a Boolean.
Copy linkTweet thisAlerts:
@islegmarauthorMay 16.2011 — Aha! Now I see it :-) Thanks a lot.

Isi
Copy linkTweet thisAlerts:
@KorMay 16.2011 — In fact JavaScript has a native method: [B]Boolean([I]variable[/I])[/B], to do that. But either the coders don't know about Boolean(), or they prefer a little bit shorter (and faster) notation.
Copy linkTweet thisAlerts:
@rnd_meMay 16.2011 — it's redundant 99% of the time; anything that is truish with "!!" is truish without it.
Copy linkTweet thisAlerts:
@KorMay 16.2011 — it's redundant 99% of the time; anything that is truish with "!!" is truish without it.[/QUOTE]
Agree. But sometimes you might need straight the Boolean evaluation. Like in setting dynamically a certain value for a property. Like checked, readOnly... things like that ?
Copy linkTweet thisAlerts:
@rnd_meMay 16.2011 — Agree. But sometimes you might need straight the Boolean evaluation. Like in setting dynamically a certain value for a property. Like checked, readOnly... things like that ?[/QUOTE]

even in those cases, "!!" still redundant and not needed.

firebug examples for this page's quick-reply box:
[CODE]
elm = $("vB_Editor_QR_textarea");
elm.readOnly="false"; //is actually==true



elm = $("qr_quickreply");
elm.checked= alert; // fn/obj is truish...
setTimeout(function(){elm.checked = 5 * 0;}, 1500); //zero is falseish.
[/CODE]
Copy linkTweet thisAlerts:
@KorMay 16.2011 — I know. But as I said, it just a matter of changing the type of the variable.
<i>
</i>var x='a';
alert(typeof(x))
alert(typeof(!!x))

Not of much use, I reckon. A defined variable is always taken as a Boolean true, if necessary, and an [B]undefined[/B] or [B]null [/B]as a Boolean true. I have just explained the OP what that double negative was for.
Copy linkTweet thisAlerts:
@rnd_meMay 16.2011 — typeof is an operator, not a function.
Copy linkTweet thisAlerts:
@KorMay 16.2011 — typeof is an operator, not a function.[/QUOTE]
Who said it is not an operator? But it is a method as well. It is not my fault that it works as a method. ? Maybe Crockford's:

http://javascript.crockford.com/remedial.html
Copy linkTweet thisAlerts:
@rnd_meMay 16.2011 — Who said it is not an operator? But it is a method as well. It is not my fault that it works as a method. ? maybe Crockford's:

http://javascript.crockford.com/remedial.html[/QUOTE]


extra parens are ignored, just like those around return values...

in the interest of not confusing noobs, as well as being a know-it-all, i just thought id' point it out. ?
×

Success!

Help @islegmar spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...