Bird
Raised Fist0
Vueframework~10 mins

v-if directive behavior in Vue - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to conditionally render the paragraph only if show is true.

Vue
<template>
  <p v-if="[1]">Hello Vue!</p>
</template>
Drag options to blanks, or click blank then click option'
Ashow
Bvisible
Cdisplay
DisShown
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name that is not defined in the component.
Forgetting to use quotes around the variable in the directive.
2fill in blank
medium

Complete the code to toggle the show variable when the button is clicked.

Vue
<template>
  <button @click="[1]">Toggle</button>
  <p v-if="show">Visible Text</p>
</template>

<script setup>
import { ref } from 'vue'
const show = ref(false)

function toggle() {
  show.value = !show.value
}
</script>
Drag options to blanks, or click blank then click option'
Ashow.value = !show.value
Btoggle
Cshow = !show
Dtoggle()
Attempts:
3 left
💡 Hint
Common Mistakes
Using the variable name instead of calling the function.
Trying to assign values directly in the template.
3fill in blank
hard

Fix the error in the template to correctly use v-if with the reactive variable show.

Vue
<template>
  <p v-if="[1]">Conditional Text</p>
</template>

<script setup>
import { ref } from 'vue'
const show = ref(true)
</script>
Drag options to blanks, or click blank then click option'
Ashow.value
Bshow
Cshow()
Dshow.value()
Attempts:
3 left
💡 Hint
Common Mistakes
Using show without .value in script but not in template.
Trying to call show as a function.
4fill in blank
hard

Fill both blanks to create a v-if that shows the message only if isLoggedIn is true and hasAccess is true.

Vue
<template>
  <p v-if="[1] && [2]">Welcome back!</p>
</template>

<script setup>
import { ref } from 'vue'
const isLoggedIn = ref(true)
const hasAccess = ref(false)
</script>
Drag options to blanks, or click blank then click option'
AisLoggedIn
BhasAccess
CisLogged
Daccess
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect variable names.
Using || instead of &&.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps each item to its length only if the length is greater than 3.

Vue
<script setup>
const items = ['apple', 'bat', 'carrot', 'dog']
const result = {
  [1]: [2] for [3] in items if [2] > 3
}
</script>
Drag options to blanks, or click blank then click option'
Aitem
Bitem.length
Dlength
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variable names.
Not using the length property correctly.

Practice

(1/5)
1. What does the v-if directive do in Vue?
easy
A. It conditionally renders an element based on a boolean expression.
B. It always shows the element but changes its color.
C. It loops through a list and renders elements.
D. It binds a click event to an element.

Solution

  1. Step 1: Understand the purpose of v-if

    v-if controls whether an element is added or removed from the page based on a condition.
  2. Step 2: Compare options

    Only It conditionally renders an element based on a boolean expression. describes conditional rendering. Others describe unrelated Vue features.
  3. Final Answer:

    It conditionally renders an element based on a boolean expression. -> Option A
  4. Quick Check:

    v-if controls rendering = C [OK]
Hint: v-if adds/removes elements based on true/false [OK]
Common Mistakes:
  • Confusing v-if with v-show which only toggles visibility
  • Thinking v-if changes styles instead of rendering
  • Mixing v-if with event handling directives
2. Which of the following is the correct syntax to use v-if in a Vue template?
easy
A.
Content
B.
Content
C.
Content
D.
Content

Solution

  1. Step 1: Recall Vue directive syntax

    Vue directives use the format v-if="expression" inside the tag.
  2. Step 2: Check each option

    Only
    Content uses the correct attribute binding syntax with quotes and equals sign.
  3. Final Answer:

    <div v-if="isVisible">Content</div> -> Option B
  4. Quick Check:

    Correct directive syntax = B [OK]
Hint: v-if uses = and quotes for condition expression [OK]
Common Mistakes:
  • Using colon instead of equals sign
  • Omitting the condition expression
  • Using parentheses instead of quotes
3. Given this Vue template snippet:
<template>
  <div>
    <p v-if="showText">Hello World</p>
  </div>
</template>

<script setup>
import { ref } from 'vue'
const showText = ref(false)
setTimeout(() => { showText.value = true }, 1000)
</script>

What will be the rendered output immediately after the page loads?
medium
A. The paragraph is rendered but hidden with CSS.
B. The paragraph with 'Hello World' is visible.
C. The paragraph is not rendered in the DOM.
D. There will be a syntax error.

Solution

  1. Step 1: Check initial value of showText

    It is set to false initially, so v-if="showText" is false.
  2. Step 2: Understand v-if behavior on false

    When false, the element is not rendered in the DOM at all.
  3. Final Answer:

    The paragraph is not rendered in the DOM. -> Option C
  4. Quick Check:

    v-if false means no element in DOM = A [OK]
Hint: v-if false removes element from DOM immediately [OK]
Common Mistakes:
  • Confusing v-if with v-show which hides but keeps element
  • Assuming element is present but invisible
  • Expecting syntax errors from valid code
4. Identify the error in this Vue template:
<template>
  <div v-if="isVisible">Visible Content</div>
  <div v-if>Always Visible</div>
</template>

<script setup>
const isVisible = true
</script>
medium
A. There is no error; the code is valid.
B. The first v-if should be v-show.
C. The variable isVisible is not reactive.
D. The second v-if is missing a condition expression.

Solution

  1. Step 1: Check usage of v-if directives

    The first v-if has a proper condition isVisible. The second v-if has no condition.
  2. Step 2: Understand v-if syntax requirements

    v-if must have a condition expression. Missing it causes a syntax error.
  3. Final Answer:

    The second v-if is missing a condition expression. -> Option D
  4. Quick Check:

    v-if requires condition = D [OK]
Hint: v-if always needs a condition expression [OK]
Common Mistakes:
  • Thinking v-if can be used without a condition
  • Confusing v-if with v-show which can be used without expression
  • Assuming non-reactive variables cause syntax errors
5. You want to conditionally render a list of items only if the list is not empty using v-if. Which of these is the best way to write the condition?
<ul>
  <li v-for="item in items" :key="item.id">{{ item.name }}</li>
</ul>

<!-- Conditional rendering here -->
hard
A.
    ...
B.
    ...
C.
    ...
D.
    ...

Solution

  1. Step 1: Understand the goal

    You want to render the list only if it has one or more items.
  2. Step 2: Evaluate each condition

      ...
    checks if items.length > 0, which means the list is not empty.
      ...
    only checks if items exists, which may be true even if empty.
      ...
    checks for null but not empty.
      ...
    checks if undefined, which is incorrect.
  3. Final Answer:

    <ul v-if="items.length > 0">...</ul> -> Option A
  4. Quick Check:

    Check list length for non-empty = A [OK]
Hint: Use items.length > 0 to check non-empty arrays [OK]
Common Mistakes:
  • Checking only if array exists, ignoring empty arrays
  • Using undefined or null checks incorrectly
  • Not using a proper condition for v-if
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard