1

I got a const in meteor containing color and text variables:

const exampleTemplate = (text, color) => ('
   Hello, this is your text:
   ${text}.
   I hope you liked it. And here comes your color:
   <span style="color: ${color}">Boom, here she is.</span>
');

const defaultColor = '#123456';
const firstColor = '#876543';
const secondColor = '#295736';

return exampleTemplate('I am a text example', defaultColor);

How can I define defaultColor as default if no other var is set? I have found this thread but I am not able to adopt its markup. I want to use defaultColor if the const color is null, false, 0, NaN or "".

Community
  • 1
  • 1
Marian Rick
  • 3,350
  • 4
  • 31
  • 67
  • 1
    `const exampleTemplate = (text, color=defaultColor)` is best practice - not sure why it would be NAN, "", or false - the above case tests if not passed - you would have to check for these in the code - and also your template literal is incorrect - should be surrounded by back ticks unless Meteor uses a different templating engine - but assume this is es2015 syntax – markyph Jul 17 '16 at 20:17
  • "*I am not able to adopt its markup*" - just use `…${ color || defaultColor }…`. – Bergi Jul 17 '16 at 20:37
  • `color` is not a `const`??? It's a function parameter (of an arrow function stored in a `const exampleTemplate` variable) and behaves just like a `var`. – Bergi Jul 17 '16 at 20:38

1 Answers1

3

You're looking for Default Parameters.

const exampleTemplate = (text, color = defaultColor) => ('
');

You'll still need to do some type-checking for all of the conditions you mentioned though.

skyline3000
  • 7,639
  • 2
  • 24
  • 33