As the goal of return !!navigator.userAgent.match(/iPad/i);
is to find out if the current device is an iPad or not, it is interesting/elegant that the return of the function is boolean (true
or false
).
The problem is that by simply calling the function navigator.userAgent.match(/iPad/i);
(without the double negation), the return will be null
or ['iPad']
(or maybe something else), instead of true
or false
.
Use the !!
is a way to force the return of that match
be converted to a Boolean value while maintaining the correct logical value for the objective in question. In other words, ['iPad']
flipped true
and null
flipped 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
That’s ES5 or ES6 ?
– Diego Souza
@Diegosouza if I’m not mistaken this Typecast is valid since 1997 (Ecmascript 1st edition).
– OnoSendai
https://udgwebdev.com/15-javascript-hacks/ see this site. Cool, I’m Newbie on JS. I’m studying to make applications using Mean.
– Diego Souza