Reputation: 59
I try to use '==' to compare enums but not works well.
if (data.event == ModalEventsEnum.SUBMIT) {
this.calculatePorcentage();
}
this.calculatePorcentage() function not executed.
Upvotes: 2
Views: 2818
Reputation: 874
Assuming that you already know how the comparison operators "==" and "===" work (see the link provided by @edtheprogrammerguy above) lets focus on other aspects of enums in TypeScript.
If no other type is specified when the enum is defined, by default the enum is numeric, and I will assume for this answer that yours is a numeric one.
When using enums, sometimes the value stored is not the enum numeric value, but the enum element name. That is most often when data is captured in UI. When the values are received in the services, mostly in backend services, we compare the value against an enum constant.
In this snipped the comparison will fail with either '==' or '===', sice we are comparing a string "SUMMIT" with a number like 1 (assuming SUMMIT is the first element):
// For example: assuming data.event = "SUMMIT", instead of data.event = 1
if (data.event == ModalEventsEnum.SUBMIT) {
this.calculatePorcentage();
}
You would need to check the data (string or number) you are receiving in the "data.event" property.
Upvotes: 1