---
title: Avoid Unintended Modification Of Variables Outside Loops
description: "Impact Area Manageability Severity High Affected Element Lightning ## Rule ID **SF-0058** ## Impact A for-in loop in which the variable name is not explicitly scoped to the enclosing scope with the `var` keyword can refer to a variable in an enclosing…"
url: "https://community-staging.qualityclouds.com/qcdocs/qc/pmd-rules/avoid-unintended-modification-of-variables-outside-loops/"
updated: "2025-03-21"
category: PMD Rules
---

# Avoid Unintended Modification Of Variables Outside Loops

Impact Area

Manageability

           Severity

High

           Affected Element

Lightning

## Rule ID

**SF-0058**

## Impact

A for-in loop in which the variable name is not explicitly scoped to the enclosing scope with the `var` keyword can refer to a variable in an enclosing scope outside the nearest enclosing scope. This will overwrite the existing value of the variable in the outer scope when the body of the for-in is evaluated.

## Remediation

Always use the `var` keyword to declare variables inside for-in loops.

### Time to fix

45 min

## References

See the PMD documentation article: [ScopeForInVariable](https://pmd.github.io/latest/pmd_rules_ecmascript_bestpractices.html#scopeforinvariable).
