js double exclamation mark code example

Example 1: double question mark javascript

//Similar to || but only returns the right-hand operand if the left-hand is null or undefined
0 ?? "other" // 0
false ?? "other" // false
null ?? "other" // "other"
undefined ?? "other" // "other"

Example 2: double exclamation mark js

// Converts anything to boolean. 

!!false === false
!!true === true

!!0 === false
!!1 === true

!!parseInt("foo") === false // NaN is falsy
!!-1 === true               // -1 is truthy
!!(1/0) === true            // Infinity is truthy

!!"" === false              // empty string is falsy
!!"foo" === true            // non-empty string is truthy
!!"false" === true          // ...even if it contains a falsy value

!!window.foo === false      // undefined is falsy
!!null === false            // null is falsy

!!{} === true               // an (empty) object is truthy
!![] === true               // an (empty) array is truthy; PHP programmers beware!

Example 3: javascript double question mark

let a = null;
const b = a ?? -1;		// Same as b = ( a != null ? a : -1 );
console.log(b);		  	// output: -1
//OR IF
let a = 9;
const b = a ?? -1;
console.log(b);  		// output: 9

//PS.,VERY CLOSE TO '||' OPERATION IN FUNCTION, BY NOT THE SAME

Example 4: es6 what is double exclamation operator

!oObject  // inverted boolean
!!oObject // non inverted boolean so true boolean representation

Example 5: javascript operator double not

// convert to boolean 
// The following examples are the only values which result in a false expression

!!0 // false
!!"" // false
!!null // false
!!undefined // false
!!NaN // false

Example 6: javascript double exclamation mark

const isIE8 = !! navigator.userAgent.match(/MSIE 8.0/);  
console.log(isIE8); // returns true or false