Skip to content

Avoid Using var for Variable Declarations

In modern JavaScript, especially within Vue components, it's crucial to avoid using var for variable declarations. The var keyword has several issues compared to let and const, including scope and hoisting problems that lead to bugs and unpredictable behavior.

📖 What is var?

The var keyword is used to declare variables in JavaScript. It was the original way to declare variables before let and const were introduced with ECMAScript 2015 (ES6).

Key Issues with var:

  • Vue’s reactivity system relies on modern JavaScript features to track changes and update the UI efficiently. var’s function-scoped nature and lack of block scope can interfere with Vue’s reactivity system, leading to problems where changes are not detected or where the code behaves unpredictably.
  • Code Maintainability: Using var makes code harder to maintain and understand. Developers often expect let and const to be used for variable declarations because they offer block-level scoping, which is more predictable and easier to reason about.

❓ Why it's good to follow this rule?

  • Predictable Behavior: let and const provide block scope, which makes variable behavior more predictable and avoids issues related to variable hoisting and function scoping.

  • Improved Reactivity: Using let and const helps maintain the integrity of Vue’s reactivity system by avoiding potential pitfalls associated with var.

  • Better Code Quality: let and const promote clearer and more maintainable code and help prevent bugs that arise from the quirks of var.

Incorrect Usage:

😱 Examples of code for which this rule will throw a warning

vue
<script setup>
var x
</script>

🤩 How to fix it?

To ensure better scoping and avoid issues related to var, use let and const instead.

Correct Usage:

vue
<script setup>
const x;
</script>