Reputation: 8681
I am trying to do an undefined check on number variable in Angular 7. Though the value is undefined it doesnt satisfy the condition. Could you tell me where I am going wrong.
public init(val: string, selectedClassficationId: number) {
this.Mode = val;
if (selectedClassficationId !== undefined) {
this.getClassificationDetails(selectedClassficationId);
}
}
Upvotes: 0
Views: 489
Reputation: 10697
Why don't you try this?
The if block will not be executed if the value for selectedClassficationId
is undefined
, null
, false
, 0
.
if (selectedClassficationId || selectedClassficationId == 0) {
this.getClassificationDetails(selectedClassficationId);
}
else {
console.log('The value is either undefined, null, false, 0', selectedClassficationId);
}
Here
is the detailed example for each use case:
Upvotes: 1
Reputation: 2327
you can try like this
public init(val: string, selectedClassficationId: number) {
this.Mode = val;
if (selectedClassficationId != null) { // != null is check null as well as undefine at the same time
this.getClassificationDetails(selectedClassficationId);
}
}
i hope it helps you out
Upvotes: 0