It’s important to remember that the purpose of template expressions is to help you write maintainable code. Keeping presentation-only expressions in the component template is a good way to do this, but not if your expressions are so complex you can’t read them. Use your best judgement for what works for you and your development team, both today, and six weeks after the last time you looked at your markup.
Additionally, while template expressions offer enhanced capabilities, certain limitations exist to maintain performance and security.
Version Requirement
Complex template expressions are supported for components with API version 66.0 and later. Components with API versions before 66.0 in their js-meta.xml file don’t support complex expressions. Expression evaluation falls back to basic property binding. Attempting to use a complex expression on a component with a lower API version will result in compiler errors.
this Keyword
The this keyword isn’t allowed in template expressions.
1<!-- ❌ Invalid -->2<div>{this.property}</div>3<!-- Error: 'this' is not allowed in template expressions -->45<!-- ✅ Valid - direct property access -->6<div>{property}</div>
1import{LightningElement}from "lwc";23export default class ThisKeywordComponent extends LightningElement{4 property = "value";5}
Function Declarations
Only arrow functions are supported, not function declarations.
1<!-- ❌ Invalid -->2<div>{function getName() { return 'John'; }()}</div>3<!-- Error: Function declarations are not allowed in template expressions -->45<!-- ✅ Valid -->6<div>{(() => 'John')()}</div>
Block Body Arrow Functions
Arrow functions with block bodies aren’t supported.
1<!-- ❌ Invalid -->2<div>{items.map(item => { return item.name; })}</div>3<!-- Error: Arrow functions with block bodies are not allowed -->45<!-- ✅ Valid -->6<div>{items.map(item => item.name)}</div>
Assignment operators aren’t allowed outside of arrow functions.
1<!-- ❌ Invalid -->2<div>{count = count + 1}</div>3<!-- Error: Assignment operators are not allowed outside arrow functions -->4<button>{count += 1}</button>5<!-- Error: Assignment operators are not allowed outside arrow functions -->
Update Operators Outside Arrow Functions
Update operators (++, --) aren’t allowed outside of arrow functions.
1<!-- ❌ Invalid -->2<div>{count++}</div>3<!-- Error: Update operators are not allowed outside arrow functions -->4<button onclick="{foo++}"></button>5<!-- Error: Update operators are not allowed outside arrow functions -->
New Operator
The new operator isn’t supported for creating instances.
1<!-- ❌ Invalid -->2<div>{new Date()}</div>3<!-- Error: 'new' operator is not allowed in template expressions -->4<button onclick="{() => new Day()}">Set Day</button>5<!-- Error: 'new' operator is not allowed in template expressions -->67<!-- ✅ Use component methods instead -->8<div>{currentDate}</div>
1import{LightningElement}from "lwc";23export default class NewOperatorComponent extends LightningElement{4 currentDate = new Date().toLocaleDateString();5}
1import{LightningElement}from "lwc";23export default class QuotingErrorsComponent extends LightningElement{4 isActive = true;5 user = {6 getName(){7 return "John Doe";8},9};1011 handleClick(){12 console.log("Clicked");13}14}
Release Preview
This release is in preview. Features described here don't become generally available until the latest general availability date that Salesforce announces for this release. Before then, and where features are noted as beta, pilot, or developer preview, we can't guarantee general availability within any particular time frame or at all. Make your purchase decisions only on the basis of generally available products and features.