this post was submitted on 05 Oct 2023
1117 points (97.0% liked)

Programmer Humor

32503 readers
506 users here now

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

founded 5 years ago
MODERATORS
 
you are viewing a single comment's thread
view the rest of the comments
[–] JackbyDev@programming.dev 1 points 1 year ago (1 children)

Please add a trigger warning to this in the future, this is too scary

[–] activ8r@sh.itjust.works 1 points 1 year ago* (last edited 1 year ago) (2 children)

In JS a Boolean has 4 states.
true
false
undefined (where the key is set to undefined)
undefined (where the key doesn't exist on the object)

if (obj.value === true) {  
    console.log(1);
} else if (obj.value === false) {
    console.log(2);
} else if ("value" in obj) { // key "value" is in the obj, but it is set to undefined 
    console.log(3);
} else { // key "value" is not in object
    console.log(4);
}

You're welcome.