Aug 21, 2022
The final example is the correct way to do it. Whew, a relief, have some claps!
Why do you use pictures of code, though? Not so useful if you want to cut and paste it.
Just a note that you look up in the table twice, however. It's a tiny bit wasteful.
A little faster is this:
if (c = countryCodes[country])
return c;
throw new Error('Invalid country')
with just one table lookup.