Vue.js Medium technical 1 views 1 min read

What is watch() and how is it used?

Peer-reviewed by HireXTech Technical Panel Updated for 2025/2026 hiring Editorial standards
Practise this track
Interviewer Expectations for this Question
01
Core Competency

Assesses fundamental understanding of Vue.js conventions, runtime behavior, and memory/performance considerations.

02
Evaluation Criteria

Hiring managers look for precision, avoidance of ambiguous jargon, and ability to explain trade-offs under real production conditions.

Comprehensive Model Answer Verified Solution

The watch() function observes a reactive source((like ref, reactive, getter functions or computed values)) and
runs a callback function whenever that source changes.

Syntax:

 watch(source, callback, options ?) 
  • source: a ref, reactive property, getter function, or array of them.
  • callback(newValue, oldValue): function to run when the value changes.
  • options (optional): object to control behavior (e.g., immediate, deep).

#### Example 1: Watching a ref

import {ref, watch} from 'vue';
    
const count = ref(0);
    
watch(count, (newVal, oldVal) => {
   console.log(`Count changed from ${oldVal} to ${newVal}`);
});
count.value = 5; // triggers the watch callback

#### Example 2: Watching a function (getter)

const firstName = ref('Sudheer');
const lastName = ref('Jonna');
    
watch(
   () => `${firstName.value} ${lastName.value}`,
   (newFullName, oldFullName) => {
       console.log(`Full name changed from ${oldFullName} to ${newFullName}`);
   }
);

#### Example 3: Watching a reactive object deeply

    import {reactive, watch} from 'vue';
   
    const user = reactive({name: 'Sudheer', age: 38});
   
    watch(
       () => user,
       (newUser, oldUser) => {
           console.log('User object changed:', newUser);
       },
       {deep: true}
    );

The main uses of watch() are:

  1. Watching route changes
  2. Triggering API calls
  3. Responding to complex data changes
  4. Manually reacting to specific state updates

****

Candidate Response Strategy & Interview Tips

  1. Start with a concise one-sentence summary: Deliver a direct, confident answer first before expanding into nuances.
  2. Demonstrate real-world trade-offs: Discuss where this approach excels and when you would avoid it in production systems.
  3. Discuss complexity & edge cases: Proactively explain time/space complexity or boundary conditions (null values, scale limits).
  4. Prepare for interviewer follow-ups: Technical hiring panels frequently probe deeper into concurrency, backward compatibility, or alternative libraries.
Related Topics & Skills
Spotted an error or have an alternative solution?