Q:

How does the Boolean return value in this JavaScript work?

I'm pretty new to programming and have been working hard to teach myself. I came across the following problem online and tried to solve it, but hit a dead end:

"Write a function that accepts three arguments and returns true if only one of those arguments is truthy, and false if not. Do not use && or || operators or if statements."

This stumped me for the past two days, so I moved on to the solution, which I'm having trouble figuring out:
function onlyOne(x, y, z) {
    return (!!x + !!y + !!z === 1);
}

I understand the syntax, but I don't understand the logic or why this works. Would someone be able to help me out? I want to learn why the code works, and not just memorize syntax.

A:

!! casts the value to an actual boolean. When you perform addition on boolean values, JavaScript coerces them into number, false being 0 and true being 1.
Basically, (!!x + !!y + !!z === 1) casts x, y, and z to true or false, then adds them all together. If only 1 of them is truthy, the value will be 1, and === 1 will be true, else it will be false.
Here is an example showing how addition with booleans works:

console.log(false + false); // 0
console.log(false + true);  // 1
console.log(true + true);   // 2

