this post was submitted on 05 Oct 2023
1097 points (96.8% liked)

Programmer Humor

37483 readers
208 users here now

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

Rules:

founded 6 years ago
MODERATORS
 
you are viewing a single comment's thread
view the rest of the comments
[–] Phen@lemmy.eco.br 4 points 2 years ago (1 children)

Except you can add the key with an undefined value and it may have different behaviors than if the key was really not there.

[–] JackbyDev@programming.dev 1 points 2 years ago (1 children)

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

[–] activ8r@sh.itjust.works 1 points 2 years ago* (last edited 2 years 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.