I have a method:
const objT2 = {
calcAge(year) {
console.log(2022 - year);
},
};
but when I use the nullish coalescing both parts is being executed.
objT2.calcAge(1990) ?? console.log(`method not found`);
//output => 32 method not found
first you are calling
calcAgemethod with1990. so by calling it, the method gets executedobjT2.calcAge(1990). this calculated2022 - 1990which is32and then logs it to the console. then this method returnsundefinedwhich gets evaluated as the left hand operand of the??operator. As this operator sees the left hand side is returning a nullish value (null or undefined), it goes on to execute the right hand side which logsmethod not foundas well.