什么时候应该使用这个例子?

when should this example be used?

给定以下代码:

if (!!a !== !!b) return false;

哪些情况下可能需要实施它而不是使用松散相等:

if( a != b ) return false;

布尔强制 (!!) 值的严格不等式 (!==)

如以下测试用例所示:

  • nullundefined 的原生对象不等价
  • 通过 toStringvalueOf 强制转换类型的其他对象取决于它们在测试真实状态时的值

备注:

  • !! 不是必需的,因为它应用于条件测试的两侧,并且该条件本身就是一个不等式测试;在这种情况下 ! 只需要将 truthy/falsey 转换为 true/false (分别);所以 test3 和 test4 是其他等效的例子,说明它是如何写的
  • 为什么 Svelte 或其他应用程序会这样写?如果有意的话,这可能是一个微优化,但这需要更多的研究来证实这个假设。
  • 参考评论中链接的(What is the !! (not not) operator in JavaScript?) that @VLAZ 也很重要。它还描述了 !! 对虚假值
  • 的影响

测试用例

function test1(a,b){
  return !!a !== !!b
}

function test2(a,b){
  return a != b
}

function test3(a,b){
  return !a !== !b
}

function test4(a,b){
  return !(!a == !b)
}

console.log('test1(0,false):', test1(0, false) );
console.log('test2(0,false):', test2(0, false) );
console.log('test3(0,false):', test3(0, false) );
console.log('test4(0,false):', test4(0, false) );
separator()

console.log('test1(\'\',false):', test1('', false) );
console.log('test2(\'\',false):', test2('', false) );
console.log('test3(\'\',false):', test3('', false) );
console.log('test4(\'\',false):', test4('', false) );
separator()

console.log('test1(null,false):', test1(null,false) );
console.log('test2(null,false):', test2(null,false) );
console.log('test3(null,false):', test3(null,false) );
console.log('test4(null,false):', test4(null,false) );
separator()

console.log('test1(undefined,false):', test1(undefined,false) );
console.log('test2(undefined,false):', test2(undefined,false) );
console.log('test3(undefined,false):', test3(undefined,false) );
console.log('test4(undefined,false):', test4(undefined,false) );
separator()

console.log('test1([],false):', test1([],false) );
console.log('test2([],false):', test2([],false) );
console.log('test3([],false):', test3([],false) );
console.log('test4([],false):', test4([],false) );
separator()

console.log('test1([\'a\'],false):', test1(['a'],false) );
console.log('test2([\'a\'],false):', test2(['a'],false) );
console.log('test3([\'a\'],false):', test3(['a'],false) );
console.log('test4([\'a\'],false):', test4(['a'],false) );
separator()

console.log('test1([\'\'],false):', test1([''],false) );
console.log('test2([\'\'],false):', test2([''],false) );
console.log('test3([\'\'],false):', test3([''],false) );
console.log('test4([\'\'],false):', test4([''],false) );
separator()

console.log('test1([0],false):', test1([0],false) );
console.log('test2([0],false):', test2([0],false) );
console.log('test3([0],false):', test3([0],false) );
console.log('test4([0],false):', test4([0],false) );
separator()

console.log('test1([1],false):', test1([1],false) );
console.log('test2([1],false):', test2([1],false) );
console.log('test3([1],false):', test3([1],false) );
console.log('test4([1],false):', test4([1],false) );
separator()

console.log('test1({},false):', test1({},false) );
console.log('test2({},false):', test2({},false) );
console.log('test3({},false):', test3({},false) );
console.log('test4({},false):', test4({},false) );
separator()

console.log('test1({a:1},false):', test1({a:1},false) );
console.log('test2({a:1},false):', test2({a:1},false) );
console.log('test3({a:1},false):', test3({a:1},false) );
console.log('test4({a:1},false):', test4({a:1},false) );

function separator(){console.log('='.repeat(30))}
.as-console-wrapper { 
  height: 100vh !important;  
  max-height: 100vh !important; 
}