Exclude `{}` While Embedding Variables In Template Literal

I apologize for this is a small change. But I think this might improve the readability of our code.

Take an example...

const name = "Ethan";
const age = 32;
console.log(`${name} is ${age} years old.`);

Here the {} surrounding the variables seems a bit unnecessary...

console.log(`$name is $age years old.`);

We could use braces while embedding an expression or while having a character or word right next to the variable...

console.log(`${name.toUpperCase()} is ${age}.`);

Dart and some other languages use this syntax. Let me know what you think.

Thank you :grinning:.

That would break existing code that assumes that a literal dollar sign will be in the resulting string, so i think it’s a nonstarter.

3 Likes

Thanks for the response. I thought about that too. What if Javascript could parse the variable after the dollar in a template string only if a variable of that name exists?

const name = "Ethan";

console.log(`Hello $name`); // Hello Ethan
console.log(`Hello $name`); // Hello $name

I think a very small number of people would have put a dollar in front of a word. But thanks :)

name could be a global variable too.