Template Expressions
A template expression is a small piece of JavaScript inside double curly braces. The expression runs and Vue shows its result.
Add two numbers
This line shows the sum of price and tax.
<p>Total: {{ price + tax }}</p>Multiply two numbers
This line shows the total cost.
<p>Cost: {{ price * quantity }}</p>Example
<script setup>
const price = 5
const tax = 2
const quantity = 3
</script>
<template>
<div>
<p>Price plus tax: {{ price + tax }}</p>
<p>Total cost: {{ price * quantity }}</p>
</div>
</template>Key points
- A template expression is a small piece of JavaScript inside double curly braces.
- An expression is JavaScript that produces one value.
- A template expression can add, subtract, or join values.
- The expression must stay short and simple.
In this example, the script block holds three plain consts. The const price holds 5. The const tax holds 2. The const quantity holds 3. The first line adds price and tax. The second line multiplies price and quantity.
Note
A template expression runs when the page shows. It does not change the const.
Try it Yourself
Edit the code, then press Run.
Running is simulated
Exercise
What does {{ price + tax }} show when price is 5 and tax is 2?