# unicorn/no-useless-undefined

> Disallow `undefined` where leaving it out means the same

A variable without a value, a `return` without a value and a missing argument are already `undefined`, and a parameter defaults to `undefined` anyway. Writing it out changes nothing.

`--fix` removes the `undefined`.

{/* Generated by src/codegen/lint-rule-docs.ts. Do not edit this section. */}

## Configure

Off by default. To turn it on:

```toml bunfig.toml
[lint.rules]
"unicorn/no-useless-undefined" = "error" # or "warn"
```

Every option, set to its default:

```toml bunfig.toml
[lint.rules]
"unicorn/no-useless-undefined" = ["error", {
  checkArguments = true,
  checkArrowFunctionBody = true,
}]
```

{/* End of the generated section. */}

## Examples

Reported:

{/* bad {"count": 5} */}
```js
let selected = undefined;

function find(id, fallback = undefined) {
  if (!id) {
    return undefined;
  }
  return load(id, undefined);
}

const noop = () => undefined;
```

Not reported:

{/* good */}
```js
let selected;

function find(id, fallback) {
  if (!id) {
    return;
  }
  return load(id);
}

const noop = () => {};
```

Sometimes you do need to write it. A `const` must have a value. A function with a return type keeps its `return undefined`, because TypeScript may require it. And you can only drop `undefined` at the end of the arguments.

{/* good */}
```ts
const missing = undefined;

function find(id: string): string | undefined {
  return undefined;
}

render(undefined, container);
```

Calls where the argument is the point aren't reported either. That covers functions whose name starts with `set`, collection methods such as `push`, `add`, `has` and `includes`, assertions such as `toBe` and `toEqual`, `createContext`, and `ref`.

{/* good */}
```js
setUser(undefined);
items.push(undefined);
expect(result).toBe(undefined);
```

## Options

| Option | Default | What it does |
| --- | --- | --- |
| `checkArguments` | `true` | Report `undefined` at the end of a call's arguments. |
| `checkArrowFunctionBody` | `true` | Report `() => undefined`. |

{/* good {"options": [{"checkArguments": false, "checkArrowFunctionBody": false}]} */}
```js
load(id, undefined);

const noop = () => undefined;
```
