0
0
Vueframework~20 mins

v-if directive behavior in Vue - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Vue v-if Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is rendered with v-if and v-else?
Consider this Vue template snippet:
<template>
  <div>
    <p v-if="showMessage">Hello!</p>
    <p v-else>Goodbye!</p>
  </div>
</template>

If the component's showMessage data property is false, what will be shown in the browser?
AThe text 'Hello!' is shown.
BThe text 'Goodbye!' is shown.
CBoth 'Hello!' and 'Goodbye!' are shown.
DNothing is shown because v-if is false.
Attempts:
2 left
💡 Hint
Remember that v-else shows content only when v-if is false.
state_output
intermediate
2:00remaining
How many paragraphs render with multiple v-if?
Given this Vue template:
<template>
  <div>
    <p v-if="count > 0">Positive</p>
    <p v-if="count < 0">Negative</p>
    <p v-if="count === 0">Zero</p>
  </div>
</template>

If count is 0, how many <p> elements will be rendered?
AThree paragraphs: 'Positive', 'Negative', and 'Zero'.
BNo paragraphs are rendered.
CTwo paragraphs: 'Positive' and 'Zero'.
DOne paragraph with text 'Zero'.
Attempts:
2 left
💡 Hint
Each v-if is independent here.
📝 Syntax
advanced
2:00remaining
Which v-if usage causes a syntax error?
Which of these Vue template snippets will cause a syntax error when using v-if?
A&lt;p v-if=isVisible&gt;Visible&lt;/p&gt;
B&lt;p v-if="isVisible &amp;&amp; !isHidden"&gt;Visible&lt;/p&gt;
C&lt;p v-if="isVisible"&gt;Visible&lt;/p&gt;
D&lt;p v-if="isVisible ? true : false"&gt;Visible&lt;/p&gt;
Attempts:
2 left
💡 Hint
Check how attribute values are quoted in HTML.
🔧 Debug
advanced
2:00remaining
Why does v-if not update when data changes?
A Vue component has this template:
<template>
  <div>
    <p v-if="show">Shown</p>
  </div>
</template>

And this script:
const show = false
setTimeout(() => {
  show = true
}, 1000)

Why does the paragraph not appear after 1 second?
ABecause setTimeout cannot update Vue data properties.
BBecause v-if only works on mounted elements, not dynamic changes.
CBecause <code>show</code> is not reactive; it should be a ref or reactive property.
DBecause the template syntax is incorrect; v-if needs a method call.
Attempts:
2 left
💡 Hint
Vue tracks reactive data to update the DOM.
🧠 Conceptual
expert
3:00remaining
What happens when v-if and v-for are used on the same element?
Consider this Vue template:
<template>
  <ul>
    <li v-for="item in items" v-if="item.visible" :key="item.id">
      {{ item.name }}
    </li>
  </ul>
</template>

What is the behavior of this code?
Av-if is evaluated for each item after v-for creates the list, so only visible items render.
Bv-if is evaluated once before v-for, so the entire list is either shown or hidden.
Cv-for is ignored because v-if takes precedence on the same element.
DThis causes a runtime error because v-if and v-for cannot be on the same element.
Attempts:
2 left
💡 Hint
Think about the order Vue processes directives on the same element.