DEV Community

Discussion on: if (!_if) what

Collapse
 
mahlongumbs profile image
Mahlon Gumbs • Edited

Edit, I misread the question. Here is the corrected response.

Sure. As @Alain mentioned, you can use an optional parameter.

Optional Parameter

const loadAnimals = () => {  
  animals = {
    dog: "LOVELY",
    cat: "CUTE",
    pony: "AWESOME",
  };
};

const getAnimals = (overrides = {}) => {
  if(!animals) loadAnimals();
  return {
    ...animals,
    ...overrides,
  };
};

getAnimals({
  dog: "GREAT",
  monkey: "AGILE",
}); // -> {dog: "GREAT", cat: "CUTE", pony: "AWESOME", monkey: "AGILE"}

Thanks for your comment.