Your example is a bit too trivial to know what you're asking. The naive answer is
function name(opt) {
return opt;
}
Are you looking for the ternary operator?
function name(opt) {
return (opt === 'cat')
? 'cat'
: (opt === 'dog')
? 'dog'
: undefined;
}
Or something like a string enum?
const Animals = {
cat: 'cat',
dog: 'dog'
};
Animals.cat; // 'cat'
Animals.dog; // 'dog'
Or a real functional programming concept like a curried function? (This is not the best example though)
function name(opt) {
return opt || (arg) => name(arg);
}
name('cat') // 'cat'
name()('dog') // 'dog'