mercredi 11 août 2021

Checking the value of an object's property that may not exist

Is there a clean way to have a branching statement checking the value of an object's property which may not exist?

Basically:

if (object.a != undefined && object.a == 1) {
    // do x
} else {
    // do y
}

What I would want to avoid is this:

if (object.a != undefined) {
    if (object.a == 1) {
        // do x
    } else {
        // do y
} else {
    // do y
}

Aucun commentaire:

Enregistrer un commentaire