When dealing with variables in JavaScript, I often need to determine if a variable is false, true, or null. If the variable is null or undefined, I want to assign an array to it by default. While this syntax works well in other languages, in JS assigning an array to myBool when the value is false can be tricky.
const boolFromBody = false;
const myBool = boolFromBody || [true, false];
console.log(myBool)
I found a workaround by checking only for null values:
const boolFromBody = null;
let otherBool = boolFromBody;
if (boolFromBody === null) {
otherBool = [true, false]
}
console.log(otherBool);
Is there a more elegant way to handle this scenario in JavaScript?