0
0
Vueframework~20 mins

JavaScript expressions in templates in Vue - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Vue Template Expression Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
Output of JavaScript expression in Vue template interpolation
What will be displayed inside the <div> when this Vue component renders?
Vue
<template>
  <div>{{ message.split('').reverse().join('') }}</div>
</template>

<script setup>
const message = 'hello'
</script>
Ahello
Bolleh
Ch e l l o
Dundefined
Attempts:
2 left
💡 Hint
Think about what the split, reverse, and join methods do to a string.
📝 Syntax
intermediate
2:00remaining
Valid JavaScript expression in Vue template
Which option is a valid JavaScript expression inside a Vue template interpolation to show the square of num?
Vue
<template>
  <div>{{ /* expression here */ }}</div>
</template>

<script setup>
const num = 4
</script>
Anum *
BMath.pow(num)
Cnum ** 2
Dsquare(num)
Attempts:
2 left
💡 Hint
Remember the exponentiation operator in JavaScript.
🔧 Debug
advanced
2:00remaining
Why does this Vue template expression cause an error?
Consider this Vue template snippet:
<div>{{ user.name.toUpperCase() }}</div>
If user is null, what error will occur and why?
Vue
<template>
  <div>{{ user.name.toUpperCase() }}</div>
</template>

<script setup>
const user = null
</script>
ATypeError: Cannot read properties of null (reading 'name')
BReferenceError: user is not defined
CSyntaxError: Unexpected token '.'
DNo error, displays empty string
Attempts:
2 left
💡 Hint
Think about what happens when you try to access a property on null.
state_output
advanced
2:00remaining
Output after reactive state change in Vue template expression
Given this Vue component, what will be the displayed output after the button is clicked once?
Vue
<template>
  <div>{{ count.value * 3 }}</div>
  <button @click="increment">Add</button>
</template>

<script setup>
import { ref } from 'vue'
const count = ref(2)
function increment() {
  count.value++
}
</script>
A9
B6
C3
Dundefined
Attempts:
2 left
💡 Hint
Remember that count is reactive and starts at 2, then increments by 1.
🧠 Conceptual
expert
2:00remaining
Understanding expression evaluation order in Vue templates
In a Vue template, what is the result of this expression if a = 5 and b = 0?
{{ a && b || a }}
Vue
<template>
  <div>{{ a && b || a }}</div>
</template>

<script setup>
const a = 5
const b = 0
</script>
Afalse
B0
Cundefined
D5
Attempts:
2 left
💡 Hint
Recall how JavaScript evaluates && and || operators from left to right.