Object dynamic property with or

# somonek (7 years ago)

consider the following example:

const myObj = { propertyOne: 'someValue', propertyTwo: 'another value', };

suppose I have to get a non existing property dynamically with a fallback on another one:

let myProp = myObj['propertyTwo']; if (myObj['propertyThree']) { myProp = myObj['propertyThree']; }

wouldn't it be nice to have a quicker way of doing that like this?

const myProp = myObj['propertyThree' || 'propertyTwo']; this will return undefined of course but some other operator could be used like: let myProp = myObj['propertyThree' ?? 'propertyTwo']; let myProp = myObj['propertyThree' ?? 'propertyFour' ?? 'propertyTwo'];

any thoughts?

# somonek (7 years ago)

probably just an alternative of doing let myProp = myObj['propertyThree'] || myObj['propertyTwo']

# T.J. Crowder (7 years ago)

FWIW, see

I'm sure there were others as well. There was an old strawman for a "default operator" but the old wiki isn't accessible (right now? anymore?).

I'd love to see ?? (and ??= for that matter), where the "falsiness" is defined as the value is null or undefined, e.g.:

a = b ?? c;

is in effect

{
    const temp = b;
    a = temp === null || temp === undefined ? c : temp;
}

IIRC, in previous discussions there were firmly-held convictions for just undefined and for both. In pure JavaScript terms, only considering undefined as the falsy value would make sense, but A) ?? is established as a null-coalescing operator elsewhere (e.g., C#), and B) JavaScript has to interoperate with typed APIs like the DOM that use null to mean "nothing." Contrived example:

function foo(elm) {
    elm ??= document.getElementById("y");
}
foo(document.getElementById("x"));

Default arguments don't work for the above because they only default on undefined, not both. And in the previous discussions, people wanted ?? in non-default-argument situations.

Given this has been kicking around for a long time, is there a will to consider it if a proper proposal is made?

-- T.J. Crowder

# Matthew Robb (7 years ago)

Why not this: const { a = o.b } = o;?

On Jun 15, 2017 4:59 AM, "T.J. Crowder" <tj.crowder at farsightsoftware.com>

wrote:

FWIW, see

I'm sure there were others as well. There was an old strawman for a "default operator" but the old wiki isn't accessible (right now? anymore?).

I'd love to see ?? (and ??= for that matter), where the "falsiness" is defined as the value is null or undefined, e.g.:

a = b ?? c;

is in effect

{
    const temp = b;
    a = temp === null || temp === undefined ? c : temp;
}

IIRC, in previous discussions there were firmly-held convictions for just undefined and for both. In pure JavaScript terms, only considering undefined as the falsy value would make sense, but A) ?? is established as a null-coalescing operator elsewhere (e.g., C#), and B) JavaScript has to interoperate with typed APIs like the DOM that use null to mean "nothing." Contrived example:

function foo(elm) {
    elm ??= document.getElementById("y");
}
foo(document.getElementById("x"));

Default arguments don't work for the above because they only default on undefined, not both. And in the previous discussions, people wanted ?? in non-default-argument situations.

Given this has been kicking around for a long time, is there a will to consider it if a proper proposal is made?

-- T.J. Crowder

# T.J. Crowder (7 years ago)

On Thu, Jun 15, 2017 at 11:19 AM, Matthew Robb <matthewwrobb at gmail.com>

wrote:

Why not this: const { a = o.b } = o;?

That works for the object-specific case from somonek (if he uses propertyThree as his/her variable instead of myProp), e.g.:

const myObj = {
    propertyOne: 'someValue',
    propertyTwo: 'another value'
};
let {propertyThree = myObj.propertyTwo} = myObj;
console.log(propertyThree); // another value
myObj.propertyThree = 'third value';
({propertyThree = myObj.propertyTwo} = myObj);
console.log(propertyThree); // third value

Doesn't help in the general case, but does address this specific case.

-- T.J. Crowder