Template literals are text literals that allow for embedded expressions. They are enclosed by backtick characters instead of single or double quotes.
`Hello World`
// returns "Hello World"
Template literals can embed quotes in a text value without having to escape.
// message is defined with a template literal
var message = `My name is "Dave"`
// is equivalent to
message = "My name is \"Dave\"";
// throws an error - Parse Error: Unexpected identifier
message = "My name is "Dave"";
New lines are preserved without needing to use the TextFormatting.NewLine constant or \n
.
var list = `Corn: 5,000
Wheat: 4,000
Rice: 2,000
Other: 500`;
// Output is the same as
list = "Corn: 5,000\nWheat: 4,000\nRice: 2,000\nOther: 500\n";
// and the same as
list = "Corn: 5,000\n" +
"Wheat: 4,000\n" +
"Rice: 2,000\n" +
"Other: 500\n";
// and the same as
list = "Corn: 5,000" + TextFormatting.NewLine
"Wheat: 4,000" + TextFormatting.NewLine
"Rice: 2,000" + TextFormatting.NewLine
"Other: 500" + TextFormatting.NewLine;
Template literals can contain placeholders for values and statements, indicated by a dollar sign and curly braces (${your
). All expressions in placeholders within template literals are evaluated and inserted as a text literal. This allows for easier text manipulation in Arcade.
`There are ${$feature.STUDENTS} students at ${$feature.UNIVERSITY}.`
// returns "There are 15,000 students at the University of California."
var scores = [70, 85, 92, 98, 100, 78, 89];
`The average score was ${Round(Average(scores), 2)}%.`
// returns "The average score was 87.43%."