1

I have a string at the top of my script with a variable in it that isn't declared yet, but I want to keep the string at the top of the script to make it easier for other users to modify. Is there any way to do that? Example:

const greeting = `Hello, ${name}. How are you?`;

Then in a completely different part of the code where the above greeting doesn't have name access:

const name = 'Steve';
sendMessage(greeting);
user1807782
  • 451
  • 1
  • 4
  • 17
  • 1
    https://stackoverflow.com/questions/42279852/format-string-template-with-variables-in-javascript/42280083#42280083 – pawel Oct 18 '17 at 19:13
  • Read the answer of @OriDrori or try defining the `name` constant before you append the string to `greeting` constant. – Mr. Alien Oct 18 '17 at 19:15

1 Answers1

2

Use an arrow function:

const greeting = (name) => `Hello, ${name}. How are you?`;

const name = 'Steve';
sendMessage(greeting(name));
Ori Drori
  • 183,571
  • 29
  • 224
  • 209