why Null Coalescing is not working with ternary operator. I would expect to get tdy.
const test = {
todo: {
day: 'tdy'
}
}
const filterDayRange = [{
day: 'mon'
}]
const result =
test.todo?.day ?? filterDayRange.length > 0 ? filterDayRange[0].day : 'tdy';
console.log(result)
// expected Output: tdy
CodePudding user response:
separate the ternary operator will fix the issue
let result = test.todo?.day ?? (filterDayRange.length > 0 ? filterDayRange[0].day : 'tdy')
CodePudding user response:
I simply add Parantheses :
test.todo?.day ?? (filterDayRange.length > 0 ? filterDayRange[0].day : 'tdy');
and now it works fine
CodePudding user response:
Everythings seems to work as expected. filterDayRange.length > 0 evaluates to truth. You can test it by replacing the result expressions in ternary operator
const test = {
todo: {
day: 'tdy'
}
}
const filterDayRange = [{
day: 'mon'
}]
const result =
test.todo?.day ?? filterDayRange.length > 0 ? 'tdy': filterDayRange[0].day;
console.log(result)