Since the purpose of return !!navigator.userAgent.match(/iPad/i);
is to find out whether the current device is an iPad or not, it is interesting / elegant that the function return is Boolean ( true
or false
).
The problem is that by simply calling the
navigator.userAgent.match(/iPad/i);
function (without the double negation), the return will be
null
or
['iPad']
(or maybe something else) instead of
true
or
false
.
Using
!!
is a way to force the return of this
match
to be converted to a Boolean value, while maintaining the correct logical value for the target in question. In other words,
['iPad']
turns
true
and
null
false
. =)
In stages it becomes clearer:
navigator.userAgent.match(/iPad/i); // ex: retorno ['iPad']
!navigator.userAgent.match(/iPad/i); // primeira negação, !['iPad'] == false
!!navigator.userAgent.match(/iPad/i); // segunda conversão, !false == true