I am just a newbie in javascript, this is how i am writing if condition in a javascript,
function setAccType(accType) {
if (accType == "PLATINUM") {
return "Platinum Customer";
} else if (accType == "GOLD") {
return "Gold Customer";
} else if (accType == "SILVER") {
return "Silver Customer";
}
},
Is there a better way to do it?
You can use an object as a map:
function setAccType(accType){
var map = {
PLATINUM: 'Platinum Customer',
GOLD: 'Gold Customer',
SILVER: 'Silver Customer'
}
return map[accType];
}
Or as @Tushar pointed out:
var accountTypeMap = {
PLATINUM: 'Platinum Customer',
GOLD: 'Gold Customer',
SILVER: 'Silver Customer'
}
function setAccType(accType){
return accountTypeMap[accType];
}
var TYPES = {
"PLATINUM":"Platinum Customer",
"GOLD":"Gold Customer",
"SILVER":"Silver Customer"
}
function getType(acctType){
return TYPES[acctType];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With