I understand what Boolean attributes are, but I am not able to understand, why would we need them. The example shown in freecodecamp is enough for me to understand, but what kind of situations would be there where we would need to use them.
booleans are the basest of values - there is only 2, true
or false
, like in bits, in binary, where you have only 1 and 0, yes and no
also, all comparisons evaluate to a boolean
5 > 2 // true
0.2 + 0.1 === 0.3 // false
6 >= 6 // true
[9,6,7].includes(5) // false
"Happy" === "Sad" // false
all values also can be classified as falsy
or truthy
, as in when the context expect a boolean but it doesn’t get one, it treates everything as a boolean anyway
falsy values are
- the number
0
- the BigInt
0n
- the keyword
null
- the keyword
undefined
- the boolean
false
- the number
NaN
- the empty string
""
(equivalent to''
or ``
everything else is truthy
where to use them?
conditions
if (false) {
// this doesn't execute
} else if (true) {
// this execute
}
2 Likes
Also I found an article on FCC news that explains booleans with the help of a court analogy.
1 Like