Skip to content
Tutorials Center
My learning

Topics

Lesson 86 of 87 ยท Practice and Review

Vue.js Interview Questions and Answers

Twenty Vue 3 interview questions with clear answers and runnable examples - reactivity, ref vs reactive, computed vs watch, lifecycle, keys, Composition vs Options, Vue vs React.

A Vue interview rarely checks whether you memorized the API. It checks whether you understand why things work the way they do: why a ref needs .value, why a computed property is cached, why a list needs keys. If you can explain those reasons in your own words and back them with a tiny example, the rest follows.

This lesson collects twenty questions in six groups, each with a short answer you could give out loud and, where it helps, a longer explanation. Three questions come with an example you can run to prove the answer to yourself. Use the headings as flash cards: read the question, answer it before you read on.

Fundamentals

1. What is Vue, and what does "progressive framework" mean?

Vue is a JavaScript framework for building user interfaces from components. "Progressive" means you can adopt it in steps: drop a script tag into an existing page and enhance one widget, or build a full single-page application with a build tool, a router and a store. You pay for the complexity you actually use. See the introduction and ways to use Vue.

2. What is declarative rendering?

You describe what the page should look like for the current state (a template with {{ }}, v-if, v-for), and Vue works out how to update the DOM when the state changes. You never call appendChild or textContent yourself.

3. What is the virtual DOM, and does Vue use it?

Vue compiles templates into render functions that return a tree of lightweight JavaScript objects (virtual nodes). On each update Vue compares the new tree with the old one and patches only the real DOM nodes that changed. Vue's compiler also marks which parts of a template are static or dynamic, so many updates skip most of the comparison work. You normally never touch the virtual DOM directly; render functions are the escape hatch.

4. What changed from Vue 2 to Vue 3?

The most important changes: the Composition API (an addition, not a replacement), a reactivity system built on Proxy, multiple root nodes per component, Teleport, better TypeScript support, smaller and faster output, and v-model changes (modelValue and update:modelValue). Vue 2 reached end of life in 2023, so new projects use Vue 3.

Reactivity

5. How does Vue's reactivity work?

Vue wraps your state in a Proxy. When code reads a property inside a render function, a computed property or a watcher, Vue records that this piece of code depends on that property (tracking). When the property is written, Vue re-runs everything that depends on it (triggering). Updates are queued and applied once per tick. The details, including a mini implementation, are in how reactivity works.

flowchart LR
  R["render / computed / watcher reads state"] -->|"track"| D["dependency map"]
  W["code writes state"] -->|"trigger"| D
  D -->|"re-run dependents"| R

Reads register dependencies; writes notify them.

6. What was the limitation of Vue 2's reactivity that Proxies solved?

Vue 2 used Object.defineProperty, which can only intercept properties that exist when the object is made. Adding a new property or setting an array item by index was not detected, so you needed Vue.set. A Proxy intercepts every operation on the object, including adding and deleting properties, so those workarounds are gone.

7. What is the difference between ref and reactive?

ref(value) holds any value (primitive or object) inside an object with a .value property. Refs can be reassigned (x.value = newThing) and passed around without losing reactivity. reactive(object) returns a proxy of an object you use without .value, but it cannot hold primitives, you must not replace it, and destructuring it breaks the link. A good default is ref for everything; reach for reactive for a group of related fields that is never replaced. Compare ref and reactive, and see the common mistakes.

8. Why do refs need .value?

A JavaScript number or string is copied when you pass it around, so nothing could observe a later write. Wrapping it in an object gives Vue a property getter and setter to track. In templates, top-level refs are unwrapped automatically, which is why {{ count }} works without .value.

9. What is the difference between computed and a method? When do you use watch?

A computed value is derived state: it is cached and only re-evaluated when something it read has changed. A method runs on every call. A watch is for side effects in response to a change (calling an API, writing to storage, logging), not for deriving values. The rule of thumb: if you can express it as "this is a function of that", use computed; if you need to do something when it changes, use watch. The example below counts how often each one runs.

Computed is cached, methods are not

On first render the computed property runs once (the second read uses the cache) and the method runs twice. Press "Change unrelated state": the page re-renders, the method runs two more times, and the computed property does not run at all because price and qty did not change. Press "Change qty": the computed property runs once more, and the watcher records the old and new value. More in computed properties and watchers.

10. What does watchEffect do differently from watch?

watch(source, callback) names its source explicitly, runs lazily (only on change unless you pass immediate) and gives you the old and new value. watchEffect(fn) runs immediately and tracks automatically whatever reactive values fn reads, so there is nothing to list but also no old value. Both are stopped automatically when the component unmounts.

11. Why does updating state not change the DOM immediately?

Vue batches updates: writes mark the component as dirty, and the render runs once on the next microtask. That means ten writes in a row cost one render. If you must read the updated DOM, await nextTick() first (see DOM update timing).

Templates and lists

12. What is key for, and why is the index a bad key?

key tells Vue which item is which when a list changes, so it can reuse, move or remove the right DOM elements and component instances. An index key says "row 0 is always the same thing", so after removing the first item, row 0's state (text in an input, a focused element) is reused for the item that moved up. Use a stable unique id. See list rendering.

13. v-if or v-show?

v-if really adds or removes the element (and runs its lifecycle hooks); it has a higher toggle cost but no cost while hidden. v-show always renders the element and toggles display: none; it is cheaper to toggle and costlier at first render. Use v-show for something toggled often, v-if when the condition rarely changes or the content should not exist at all. v-show does not work on <template> and has no v-else.

14. Why should you not use v-if and v-for on the same element?

In Vue 3 v-if is evaluated first and cannot see the loop variable. Filter with a computed property instead, or wrap the loop in a <template>.

Components

15. How do components communicate?

Parent to child: props. Child to parent: events (emit). Between distant components: provide / inject, or a shared store. Two-way binding on a component is props plus events, packaged as v-model (defineModel in modern Vue). Props are readonly: a child must never write to them. See props, emits and provide and inject.

16. What are slots?

Slots let a parent pass template content into a child, so the child controls the layout and the parent controls the content. Named slots give several insertion points, and scoped slots let the child pass data back out to the slot content.

17. In what order do lifecycle hooks run?

For a parent that contains a child, mounting runs down and then up: the parent's setup and onBeforeMount, then the child's setup, onBeforeMount and onMounted, and only then the parent's onMounted, since a parent is only mounted when all its children are. Unmounting follows the same "parent before, children, parent after" shape.

flowchart TD
  A["setup()"] --> B["onBeforeMount"] --> C["render and insert into the DOM"] --> D["onMounted"]
  D --> E["state changes"] --> F["onBeforeUpdate"] --> G["DOM patched"] --> H["onUpdated"] --> E
  D --> I["onBeforeUnmount"] --> J["onUnmounted"]

One component's life: setup, mount, any number of updates, unmount.

Lifecycle order of a parent and a child

Read the list top to bottom: the child finishes mounting before the parent's onMounted. Press the button to see the child's two unmount hooks appended. More in lifecycle hooks.

18. How do you reuse stateful logic between components?

With composables: plain functions, named useSomething, that create refs, watchers and lifecycle hooks and return what the component needs. They replaced mixins, which hid where properties came from and could clash by name. See composables.

Composition API versus Options API

19. What are the differences between the Composition API and the Options API, and when do you pick which?

The Options API groups code by type (data, methods, computed, watch) and exposes everything on this. The Composition API groups code by feature: related state, computed values and watchers sit together and can be moved into a composable. Composition gives better TypeScript inference and easier reuse; Options is easy to learn and still fully supported. They can be mixed in one component, and both run on the same reactivity system. With <script setup>, the modern default, the Composition API needs almost no boilerplate. Here is the same counter written both ways:

The same counter in both APIs

Both buttons behave identically. In a real project the second would be a .vue file with <script setup> and no return. See the Options API and script setup.

Vue versus React and the wider picture

20. How does Vue compare with React?

Both are component-based and use a virtual DOM. The main differences:

Vue React
Templates HTML-like templates (JSX also possible) JSX only
State updates Mutate reactive state; Vue tracks who read it Call a setter with new immutable state; React re-renders the component
Re-render scope Only components that read the changed state The component and (by default) its children
Derived values computed is cached automatically useMemo, written by hand with a dependency array
Effects watch / watchEffect track dependencies for you useEffect with a dependency array you maintain
Two-way binding v-model built in Controlled inputs written by hand
Official ecosystem Router and store (Pinia) maintained by the Vue team Community choices (React Router, Redux, Zustand...)

Neither is "better"; they make different trade-offs. Vue's tracking removes whole classes of dependency-array bugs, React's explicit model is very flexible and has a huge ecosystem. Our React tutorial teaches the other side if you want to compare.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Which statement about computed properties is correct?

    Computed properties are lazy and cached. Calling a method runs it every time, and side effects such as API calls belong in watchers.
  2. 2. In which order do these hooks run when a parent mounts a child?

    A parent counts as mounted only after all its children are mounted, so the child's onMounted fires first.
  3. 3. Why did Vue 3 switch from Object.defineProperty to Proxy for reactivity?

    defineProperty can only observe properties that exist when the object is created, which is why Vue 2 needed Vue.set. A Proxy intercepts every operation on the object.
  4. 4. Which is the best answer to 'when would you use watch instead of computed'?

    computed derives values; watch runs side effects. If you can state it as 'this is a function of that', prefer computed.
  5. 5. What is the main reason index keys are risky in v-for?

    The index is a position, not an identity. When items shift, Vue reuses DOM and component state by position.

Summary

  • Vue tracks which code reads which reactive property and re-runs only that code when the property is written; Proxies make this complete.
  • ref holds any value behind .value and can be replaced; reactive is for objects you never replace or destructure.
  • computed derives cached values, watch performs side effects, watchEffect tracks automatically.
  • DOM updates are batched; use nextTick to read the DOM after a change.
  • Keys are identities: use stable ids, v-if for rare toggles and v-show for frequent ones, and never v-if with v-for on one element.
  • Props go down, events go up, v-model combines both; composables reuse stateful logic.
  • Mounting runs child hooks before the parent's onMounted.
  • Composition groups code by feature, Options by type, and Vue's automatic tracking is its main difference from React.

Next: Vue.js Cheat Sheet