I am facing an issue with a class containing an optional field called startDateHour
:
export class Test {
startDateHour?: number;
// more fields, constructor etc.
}
I need to perform an action only if the startDateHour
exists:
if (test.startDateHour) {
//do something
}
The problem arises when startDateHour
is set to 0. Even though it exists and I want to run the code, the check if (startDateHour)
returns false in this scenario.
One possible solution could be:
if (test.startDateHour || test.startDateHour === 0) {
// do something
}
Is there a way to improve this check so that it handles both scenarios effectively?