Skip to content

fix: Unexpected var, use let or const instead.#1

Open
ciadoh wants to merge 1 commit into
masterfrom
sonar-fix/77ebfe85-8e6d-46ec-a2f0-6f09c92fe713
Open

fix: Unexpected var, use let or const instead.#1
ciadoh wants to merge 1 commit into
masterfrom
sonar-fix/77ebfe85-8e6d-46ec-a2f0-6f09c92fe713

Conversation

@ciadoh

@ciadoh ciadoh commented Jun 12, 2026

Copy link
Copy Markdown
Owner

SonarQube Issue

Key: 77ebfe85-8e6d-46ec-a2f0-6f09c92fe713
File: examples/auth/index.js

AI-Suggested Fix

1. Explanation of the Issue

What this issue means:
The SonarQube static analysis tool has flagged the use of the var keyword in your JavaScript code, which is considered a code smell. The rule javascript:S3504 advises against using var because it has different scoping rules compared to let and const.

Why it matters:

  • Scoping: var is function-scoped, which can lead to unexpected behavior and bugs if not handled carefully. Variables declared with var are hoisted to the top of their function scope, which can lead to situations where the variable is accessible before it is declared.
  • Maintainability: Using let and const makes the code more predictable and easier to understand, as they are block-scoped. This reduces the likelihood of bugs related to variable scoping.
  • Best Practices: Modern JavaScript (ES6 and beyond) recommends using let for variables that can be reassigned and const for variables that should not be reassigned, promoting better coding practices.

2. Concrete Fix with a Short Code Example

Original Code:

function example() {
    var x = 10;
    if (true) {
        var x = 20;  // This will overwrite the outer `x`
        console.log(x);  // 20
    }
    console.log(x);  // 20, not 10 as one might expect
}

Fixed Code:

function example() {
    let x = 10;
    if (true) {
        let x = 20;  // This does not overwrite the outer `x`
        console.log(x);  // 20
    }
    console.log(x);  // 10
}

If the variable should not be reassigned at all:

function example() {
    const x = 10;
    if (true) {
        const x = 20;  // This does not overwrite the outer `x`
        console.log(x);  // 20
    }
    console.log(x);  // 10
}

3. Caveats or Edge Cases

  • Hoisting: Be mindful that var declarations are hoisted to the top of their function scope, whereas let and const are not. This can lead to subtle bugs if you're not aware of this difference.
  • Legacy Code: If you're working in a legacy codebase, changing var to let or const might break existing functionality if the scoping behavior was relied upon.
  • Transpilers: Ensure that your build process (e.g., Babel) is correctly configured to handle ES6 syntax if you're targeting environments that do not natively support it.
  • Global Scope: In the global scope, var declarations become properties of the global object (e.g., window in browsers). Using let or const in the global scope avoids this behavior.

By following these guidelines, you can improve the maintainability and predictability of your JavaScript code.


Generated by TechDebt AI

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant