Skip to content

Code Refactoring

Biswajit Sundara edited this page Mar 3, 2021 · 2 revisions

Alternative Of Switch

We can make use of objects instead of switch and achieve the same result. Object will look more cleaner and easy to maintain however please note switch case is the best in terms of performance.

function getCurrency(country) {
    var currency = {
        'India': 'Rupee',
        'United States': 'Dollar',
        'Europe': 'Euro',
        'default': 'Invalid Choice'
    };
    return (currency[country] || currency['default']);
}

let currency = getCurrency('India');
console.log(currency);

Clone this wiki locally