# no-top-level-assignment-in-function πŸ“ Disallow assigning to top-level variables from inside functions. πŸ’ΌπŸš« This rule is enabled in the βœ… `recommended` [config](https://github.com/sindresorhus/eslint-plugin-unicorn#recommended-config). This rule is _disabled_ in the β˜‘οΈ `unopinionated` [config](https://github.com/sindresorhus/eslint-plugin-unicorn#recommended-config). Assigning to top-level variables from inside functions creates hidden shared state. A function can then change the result of later calls, or break when it becomes recursive, reentrant, or async. Keep state local to the function, pass it explicitly, return it, or put intentional shared state in an object. If you truly need a module-level cache or other shared state, use an ESLint disable comment. This rule only reports direct writes to top-level bindings. It ignores property mutations and variables from non-top-level outer scopes. ## Examples ```js // ❌ let cache; function build() { cache = new Map(); } ``` ```js // βœ… function build() { const cache = new Map(); } ``` ```js // ❌ let index = 0; const next = () => index++; ``` ```js // βœ… const state = { index: 0, }; const next = () => state.index++; ```