Sobes.tech
Intern

What are the numerical values for the boolean values true and false?

sobes.tech AI

Answer from AI

In JavaScript, true is implicitly converted to 1, and false to 0 when used in a numeric context.

For example:

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

// Subtraction
console.log(true - 1); // 0
console.log(false - 1); // -1

// Comparison
console.log(true == 1); // true
console.log(false == 0); // true

This happens due to the type coercion mechanism. JavaScript tries to convert the boolean value to a number to perform the operation.

However, it is important to remember that this is implicit conversion. Explicitly, Boolean(1) returns true, and Boolean(0) returns false. Also, Number(true) returns 1, and Number(false) returns 0.

console.log(Number(true)); // 1
console.log(Number(false)); // 0