0

I am trying to interpolate variables in strings in JavaScript. I want to interpolate the words_to_be_removed variable with the defaultMessageTest string. I have tried the below 3 methods but none of them working.

  1. String text ${expression}
  2. "String text ${expression}"
  3. "String text " + expression " String text"
function remove_alert_word(alert_word) {
    const words_to_be_removed = [alert_word];
    const defaultMessageTest = "Alert word ${words_to_be_removed} removed successfully!";
    channel.del({
        url: "/json/users/me/alert_words",
        data: {alert_words: JSON.stringify(words_to_be_removed)},
        success() {
            update_alert_word_status(
                $t({defaultMessage: defaultMessageTest}),
                false,
            );
        },
        error() {
            update_alert_word_status($t({defaultMessage: "Error removing alert word!"}), true);
        },
    });
}
Het Patel
  • 109
  • 7

1 Answers1

1

Please use backtick characters (`)

const defaultMessageTest = `Alert word ${words_to_be_removed} removed successfully!`;

const test = 123;
console.log(`this is test${test}`)
Roman Gavrilov
  • 620
  • 4
  • 17