1117
you are viewing a single comment's thread
view the rest of the comments
[-] JackbyDev@programming.dev 7 points 1 year ago

This is JavaScript undefined logic specifically where it means the key isn't present on the object.

[-] Phen@lemmy.eco.br 4 points 1 year ago

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 1 year ago

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)

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.

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

Programmer Humor

32155 readers
212 users here now

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

Rules:

founded 5 years ago
MODERATORS