Question presented to candidate:
"Both v-if and v-show hide things in a Vue template. What is actually different between them, and how do you decide which one to use?"
What a strong answer should cover:
v-ifis real conditional rendering: when false the element is not in the DOM (Vue leaves a<!--v-if-->comment placeholder), and a component inside is unmounted, losing its local state; when true it is created and mounted again.v-showalways renders the element once and only toggles the inline styledisplay: none; components stay mounted and keep their state.- Cost trade-off:
v-ifhas a higher toggle cost but no initial cost when false (lazy);v-showhas a higher initial cost but cheap toggles. - Rule of thumb:
v-showfor things toggled often (tabs, dropdowns, accordions);v-iffor things rarely shown, or when content should not exist at all (permissions, unmet data). - Only
v-ifsupportsv-else-if/v-elseand works on<template>;v-showdoes neither. - Neither is a security boundary on its own:
v-showcontent is still in the DOM, so never use it to hide data the user must not see.
Clarifying questions expected:
- "How often does this toggle, and is the hidden part expensive to render?"
- "Does the hidden part hold state, such as a half-filled form, that must survive?"
Code / implementation expected: Optional. A small demo of the DOM and lifecycle difference is a strong answer.