Cómo verificar si una variable es verdadera o falsa en JavaScript
/*
A variables value can be an actual boolean like "true" / "false"
or it can be of another type like string or array which can be evaluated as "truthy" / "falsy".
Truthy: https://developer.mozilla.org/de/docs/Glossary/Truthy
Falsy: https://developer.mozilla.org/de/docs/Glossary/Falsy
*/
// Independent check
if (myVar) {
// entered if myVars value is either a boolean of value "true" or a value evaluated as "truthy"
}
// true/false check
if (typeof myVar === "boolean" && myVar) {
// entered if myVars value is an actual boolean and if this boolean is "true"
}
// truthy/falsy check
if (typeof myVar !== "boolean" && myVar) {
// entered if myVars value is not an actual boolean but if it's evaluated as "truthy"
}
Pl0erre