Tom
Tom

Reputation: 8681

Check for undefined failing in Angular 7

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

Answers (2)

Prashant Pimpale
Prashant Pimpale

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

Yash Rami
Yash Rami

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

Related Questions