Live data from Hacker News

No-Bullshit Guide to Detecting Everything in HTML5

diveintohtml5.org

1–10 of 37 posts

Re: No-Bullshit Guide to Detecting Everything in HTML5

#6

Excuse my ignorance, what's the !! for?

It makes sure that it returns a boolean value as opposed to something truthy/falsy which could lead to oddness if you try and directly compare it with a boolean value.

According to the spec, media.canPlayType returns either "", "maybe" or "probably". An empty string "" is falsy in Javascript and a non-empty string is truthy, but if you try and compare them directly with booleans then you can get some odd behaviour:

  if ("yes"){ // true }
  if ("yes" == true){ // false }
  if ("yes" === true){ // false }

  if (""){ // false }
  if ("" == false){ // true }
  if ("" === false){ // false }
I personally prefer to make sure that any function which you're expecting the return value to be used as a boolean actually returns a boolean value. I know there are many people out there with a dislike of !! though.

Re: No-Bullshit Guide to Detecting Everything in HTML5

#7

Excuse my ignorance, what's the !! for?

Canonicalization, so that e.g. null and undefined turn into false and ordinary values (e.g. functions, arrays, numbers, objects) turn into true.

Try putting things like these into the address bar of your browser:

  javascript:alert(!!null)
  javascript:alert(!!undefined)
  javascript:alert(!!{})
  javascript:alert(!!Math.floor)
Post reply on HN