How to only call left side of comma (,) separator

53 Views Asked by At

const test = (
    (success) => {
        console.log('SUCCESS CALLED');
    },
    (error) => { 
        console.log('ERROR CALLED');
    }
);

test(); // output is **ERROR CALLED**

In the above javascript code, is there anyway to output SUCCESS CALLED without changing the definition

1

There are 1 best solutions below

0
ShadowRanger On

There is no way to do this

The definition of the comma operator is that it evaluates the expressions in order, from left-to-right, and the overall expression is the result of the final expression evaluated (the right-most). test never contains any information about the success-oriented function, so there is no way to make test invoke it without altering something in the definition of test.