# no-undefined-assignment

Rule `sonarjs/no-undefined-assignment`. "undefined" should not be assigned

- Source: sonarjs
- Technologies: JavaScript
- Kind: style
- Needs type information: no
- Autofix: no
- Reach: 0 weekly downloads of presets that enable it
- Trend: 0% last quarter
- Rank: #2165 by reach

## Linters

| Linter | Rule name | Recommended | Autofix | Details | Docs |
| --- | --- | --- | --- | --- | --- |
| ESLint | `sonarjs/no-undefined-assignment` | no | no | package `eslint-plugin-sonarjs` | [docs](https://rules.sonarsource.com/javascript/RSPEC-2138/) |

## Code examples

### Incorrect: the rule reports this

```js
var myObject = {};

// ...
myObject.fname = undefined;  // Noncompliant
// ...

if (myObject.lname == undefined) {
  // property not yet created
}
if (myObject.fname == undefined) {
  // no real way of knowing the true state of myObject.fname
}
```

### Correct: the rule accepts this

```js
var myObject = {};

// ...
myObject.fname = null;
// ...

if (myObject.lname == undefined) {
  // property not yet created
}
if (myObject.fname == undefined) {
  // no real way of knowing the true state of myObject.fname
}
```

Examples from the ESLint documentation.

## Enable it

### ESLint: `eslint.config.js`

```js
export default [
  {
    rules: {
      "sonarjs/no-undefined-assignment": "error",
    },
  },
];
```

## Related rules

- [sonarjs/no-sonar-comments](https://lintradar.dev/rules/sonarjs/no-sonar-comments.md): Track uses of "NOSONAR" comments
- [sonarjs/no-unused-function-argument](https://lintradar.dev/rules/sonarjs/no-unused-function-argument.md): Unused function parameters should be removed
- [sonarjs/no-return-type-any](https://lintradar.dev/rules/sonarjs/no-return-type-any.md): Primitive return types should be used
- [sonarjs/no-variable-usage-before-declaration](https://lintradar.dev/rules/sonarjs/no-variable-usage-before-declaration.md): Variables declared with "var" should be declared before they are used
- [sonarjs/no-require-or-define](https://lintradar.dev/rules/sonarjs/no-require-or-define.md): "import" should be used to include external code
- [sonarjs/no-wildcard-import](https://lintradar.dev/rules/sonarjs/no-wildcard-import.md): Wildcard imports should not be used

---

Web page: https://lintradar.dev/rules/sonarjs/no-undefined-assignment. Data from npm and the linters' documentation, collected Dec 4, 2026. Site guide for agents: https://lintradar.dev/llms.txt
