What is the !! (not not) operator in JavaScript? -
i saw code seems use operator don't recognize, in form of 2 exclamation points, so: !!
. can please tell me operator does?
the context in saw was,
this.vertical = vertical !== undefined ? !!vertical : this.vertical;
coerces oobject
boolean. if falsey (e.g. 0, null
, undefined
, etc.), false
, otherwise, true
.
!oobject //inverted boolean !!oobject //non inverted boolean true boolean representation
so !!
not operator, it's !
operator twice.
real world example "test ie version":
let isie8 = false; isie8 = !! navigator.useragent.match(/msie 8.0/); console.log(isie8); // returns true or false
if =>
console.log(navigator.useragent.match(/msie 8.0/)); // returns null
but if =>
console.log(!!navigator.useragent.match(/msie 8.0/)); // returns true or false
Comments
Post a Comment