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 es 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 ""
.
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 es 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 ""
.
-
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 Commented Jul 17, 2016 at 20:17 -
"I am not able to adopt its markup" - just use
…${ color || defaultColor }…
. – Bergi Commented Jul 17, 2016 at 20:37 -
color
is not aconst
??? It's a function parameter (of an arrow function stored in aconst exampleTemplate
variable) and behaves just like avar
. – Bergi Commented Jul 17, 2016 at 20:38
1 Answer
Reset to default 3You'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.