0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

[JavaScript] 論理値 boolean の排他的論理和 XOR は !== 演算子

0
Last updated at Posted at 2025-09-04

1. 論理値のみの場合

排他的論理和は 2 値が異なる場合に真になり、2 値が等しい場合に偽になる演算のため、(厳密) 不等価演算子を用いて計算可能です。

const z = x !== y;
x y x !== y
false false false
false true true
true false true
true true false

参考「ビット排他的論理和 (^) - JavaScript | MDN
参考「厳密不等価 (!==) - JavaScript | MDN

2. 論理値以外も含む場合

論理値以外を論理値に変換して計算したい場合、2 値をそれぞれ論理否定すると簡単に計算できます。

const z = ! x !== ! y;
x y ! x !== ! y
0 0 false
0 42 true
23 0 true
23 42 false

x y ! x !== ! y
'' '' false
'' 'bar' true
'foo' '' true
'foo' 'bar' false

x y ! x !== ! y
null null false
null {} true
[] null true
[] {} false

参考「論理否定 (!) - JavaScript | MDN
参考「Falsy (偽値) - MDN Web Docs 用語集 | MDN
参考「Truthy (真値) - MDN Web Docs 用語集 | MDN

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?