How to Use the XOR Operator in JavaScript?

Estimated read time 1 min read

The XOR operator (^) in JavaScript performs a bitwise XOR operation between two values. It returns true if exactly one of its operands is true, and false otherwise.

Here is an example to demonstrate how to use the XOR operator in JavaScript:

console.log(true ^ true); // Output: false
console.log(true ^ false); // Output: true
console.log(false ^ true); // Output: true
console.log(false ^ false); // Output: false

In this example, the XOR operator is used to perform a bitwise XOR operation between two values. The result of the operation is logged to the console.

Note that the XOR operator operates on the underlying binary representation of the operands, so it is generally not appropriate for use with boolean values. For boolean values, it’s better to use the logical XOR operator !=.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply