0

working on a page basic example, I found this piece of code :

function hasGetUserMedia() {

return !!(navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia);

} 

and been astonished by : return!!(exp)

Can someone explain this '!!' ? is it for the line break ? or to avoid a return value ?

I could not find usage nor info about this kind of (weird) syntax

thanks !

Pointy
  • 405,095
  • 59
  • 585
  • 614
  • ok sorry for the duplication and many thanks for precisions , forcing a return value type is both tricky and useful ! ... should have thought about it sooner ... – user3215919 Jan 20 '14 at 18:52

2 Answers2

1

The !! coerces the value into a boolean that represents whether the original value is "truthy" or "falsy". For example:

!!"foo" // true
!!""    // false

Given that ! is the negation operator, using it once will convert a value into a boolean that is the opposite of it's truthy/falsy value. E.g.,

!"foo" // false
!""    // true

Adding another ! negates the negation, resulting in a boolean that matches the truthy/falsy-ness of the original non-boolean value.

jmar777
  • 38,796
  • 11
  • 66
  • 64
0

The code below forces the expression to be converted to boolean.

function hasGetUserMedia() {

    return !!(navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia);

}
php-dev
  • 6,998
  • 4
  • 24
  • 38