In Unicode, you can toggle between ASCII uppercase and lowercase by `XOR`'ing with hexadecimal `0x20`.
This ASCII trick wasn't intentional.
Rather, it's an artefact of the characters being 32 positions appart from their counterpart, thus only differing in the fifth bit.
`XOR`'ing with `0x20` toggles the fifth bit between `0` and `1`.
Historically, this was used to toggle between cases without the need for a conditional check.
```js
function toggleCaseWithXOR(str) {
let result = "";
for (let i = 0; i < str.length; i++) {
const asciiCode = str.charCodeAt(i);
const toggledAsciiCode = asciiCode ^ 0x20;
result += String.fromCharCode(toggledAsciiCode);
}
return result;
}
const testString = "Hello World !";
console.log("Original String:", testString);
console.log("Toggled String:", toggleCaseWithXOR(testString));
```
Source: https://www.youtube.com/shorts/MQp2HRZHFBA