Ternary Operator True/False

isNightTime = 1;

isNightTime?console.log(‘Turn on the lights!’): console.log(‘Turn off the lights!’);
//logs ‘Turn on the lights!’ to the console.

This is my code. I have two questions:
Is true or false correlated with the numbers 1 and 0, respectively?
Also, can ternary operators be only used when there’s a boolean condition of true or false?

1 evaluated as boolean gives true
and true evaluated as number gives 1 (this gives that true + true equals 2)
false has same relationship with 0

but, the ternary operator evaluates as a boolean whatever is the condition. some things are truthy values and evaluate to true, and some things are falsy values and evaluate to false.

falsy values are for example: empty string, null, undefined, 0, NaN

some things are falsy values, and everything else is truthy

1 Like