๐Ÿš€ LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
๐ŸŽ“ COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
HTML MASTER CLASS /// LEARN TAGS /// BUILD STRUCTURE /// SEMANTIC WEB /// HTML MASTER CLASS /// LEARN TAGS ///

Computed Properties in Vue.js

Learn about Computed Properties in this comprehensive Vue.js tutorial. Learn how to use computed properties to calculate derived state and improve application performance through caching.

โšก Total XP: 0|๐Ÿ’ป vuejs XP: 0

Skill Matrix

UNLOCK NODES BY LEARNING NEW TAGS.

Select an unlocked node to view details root

๐Ÿš€ LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
๐ŸŽ“ COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.

Derive state intelligently with computed properties.

1What is a Computed Property?

A computed property allows you to define a property that is used the same way as data, but can also have some custom logic that is evaluated to get its value. It is defined using the computed() function.

2The Magic of Caching

The main difference between a computed property and a normal function is caching. A computed property only re-evaluates when its reactive dependencies change. If you call a computed property 100 times, the logic only runs once, saving CPU cycles.

3Read-Only by Default

Computed properties should be treated as derived state, meaning you shouldn't mutate them directly. They are essentially 'getters'. (Though Vue does allow you to define writable computed properties by providing a set() function if truly needed).

4Step-by-Step Breakdown

Sometimes you need to derive new data from your existing reactive state. For example, if you have firstName and lastName, you might want fullName.

You could write a function to calculate this every time, but Vue offers a much better solution: computed properties.

A computed property tracks its dependencies. It only recalculates when one of those dependencies (like firstName) changes. Otherwise, it returns a cached result!

What is the main performance advantage of using computed over a standard function?

  • โ†’It runs asynchronously
  • โ†’It caches the result
  • โ†’It uses less memory

In the template, you use a computed property exactly like a normal ref. You do NOT add parentheses () because it is not a method call.

How do you correctly output the value of a computed property named totalPrice in the template?

  • โ†’{{ totalPrice() }}
  • โ†’{{ totalPrice.value }}
  • โ†’{{ totalPrice }}

Computed properties are extremely useful for filtering or sorting arrays without mutating the original array.

By default, computed properties are read-only. If you try to assign a value directly to fullName.value = "Jane Doe", Vue will throw a warning.

By default, are computed properties writable?

  • โ†’Yes, they are writable
  • โ†’No, they are read-only

Use computed whenever you have complex logic in your template. It keeps your templates clean and your app blazingly fast.

Level Up ๐Ÿš€

Advanced cheat sheets, SEO tricks, and interview prep for this topic.

Browser Support

ChromeSupported

Fully supported.

FirefoxSupported

Fully supported.

SafariSupported

Fully supported.

EdgeSupported

Fully supported.

Accessibility (A11y)

1Computed ARIA State

Deriving aria-expanded, aria-pressed, or aria-selected from a computed property (const ariaExpanded = computed(() => isOpen.value ? 'true' : 'false')) guarantees the attribute can never drift out of sync with the actual UI state the way a manually duplicated string literal could.

<button :aria-expanded="ariaExpanded">Menu</button>

SEO Implications

  • 1

    Computed Properties Run Identically During SSR

    Because computed() is pure synchronous JavaScript with no DOM access, it evaluates the same way on the server during server-side rendering as in the browser, so any text derived from a computed property โ€” a formatted price, a pluralized label โ€” is present in the very first HTML response a crawler downloads.

Best Practices

Keep Computed Getters Pure

A computed getter should never have side effects โ€” no API calls, no mutating other refs. Vue may re-evaluate a computed getter multiple times without a corresponding re-render, so side effects inside it can run an unpredictable number of times.

Prefer computed() Over a Method for Derived State

If a value only depends on other reactive state and takes no arguments, use computed() instead of a plain function so Vue can cache the result and skip recalculation on renders where the dependencies haven't changed.

Frequent Bugs

THE BUG

Calling a computed property like a function: {{ fullName() }}.

THE FIX

A computed ref is accessed like a property, not invoked like a method. Writing fullName() in the template throws or returns undefined depending on the value โ€” remove the parentheses and reference {{ fullName }} directly.

Real-World Examples

Filtering and Sorting a Product List

An e-commerce listing keeps the raw products array untouched and derives the visible list with a single computed(() => products.value.filter(p => p.inStock).sort((a,b) => a.price - b.price)), so toggling 'sort by price' never mutates the original fetched data.

const sortedInStock = computed(() =>
  products.value
    .filter(p => p.inStock)
    .sort((a, b) => a.price - b.price)
);

Interview Prep

?Frequently Asked Questions

Pascual Vila

Pascual Vila

Frontend Instructor // Code Syllabus

Common Pitfalls & Errors

The Error //

Mutating a Prop Directly

// Wrong const props = defineProps(['count']); function increment() { props.count++; } // Correct const emit = defineEmits(['update:count']); function increment() { emit('update:count', props.count + 1); }

The Solution //

A child component should never assign to a prop it received (e.g. props.title = 'New'). Vue logs "Unexpected mutation of prop" because props are a one-way binding. Copy the prop into local state with ref()/computed() first, or emit an event asking the parent to change its own data.

The Error //

Losing Reactivity by Destructuring reactive()

// Wrong const { name } = reactive({ name: 'Alice' }); // name is now a static string // Correct const state = reactive({ name: 'Alice' }); const { name } = toRefs(state); // name is a live ref

The Solution //

Destructuring a reactive() object copies its properties out as disconnected plain values, so future mutations of the original object won't update the destructured variables. Keep referencing the object's properties directly, or convert it with toRefs() before destructuring.

Continue Learning