# prefer-location-assign

Rule `unicorn/prefer-location-assign`. Prefer `location.assign()` over assigning to `location.href`.

- Source: unicorn
- Technologies: JavaScript
- Kind: style
- Needs type information: no
- Autofix: yes, in at least one linter
- Reach: 9.9M weekly downloads of presets that enable it
- Trend: +28.3% last quarter
- Rank: #1071 by reach, #1326 by growth

## Linters

| Linter | Rule name | Recommended | Autofix | Details | Docs |
| --- | --- | --- | --- | --- | --- |
| ESLint | `unicorn/prefer-location-assign` | no | safe | package `eslint-plugin-unicorn` | [docs](https://github.com/sindresorhus/eslint-plugin-unicorn/blob/v80.0.0/docs/rules/prefer-location-assign.md) |

## Code examples

### Incorrect: the rule reports this

```js
location.href = url;
```

### Correct: the rule accepts this

```js
location.assign(url);
```

```js
const loginUrl = 'https://example.com/login';
location.assign(loginUrl);
```

```js
if (isLoggedOut) {
  location.replace('/login'); // Can't go back to protected page
}
```

Examples from the ESLint documentation.

## Enable it

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

```js
import unicorn from "eslint-plugin-unicorn";

export default [
  {
    plugins: { unicorn },
    rules: {
      "unicorn/prefer-location-assign": "error",
    },
  },
];
```

## Presets that enable it

| Preset | Linter | Package | Rules | Weekly downloads | Trend |
| --- | --- | --- | --- | --- | --- |
| [Unicorn recommended](https://lintradar.dev/presets/unicorn-recommended.md) | ESLint | `eslint-plugin-unicorn` | 340 | 9.5M | +30.3% |
| [XO](https://lintradar.dev/presets/xo.md) | ESLint | `eslint-config-xo` | 618 | 413K | −5.0% |

## Related rules

- [unicorn/prefer-iterator-to-array](https://lintradar.dev/rules/unicorn/prefer-iterator-to-array.md): Prefer `Iterator#toArray()` over temporary arrays from iterator spreads.
- [unicorn/prefer-number-is-safe-integer](https://lintradar.dev/rules/unicorn/prefer-number-is-safe-integer.md): Prefer `Number.isSafeInteger()` over integer checks.
- [unicorn/prefer-includes-over-repeated-comparisons](https://lintradar.dev/rules/unicorn/prefer-includes-over-repeated-comparisons.md): Prefer `.includes()` over repeated equality comparisons.
- [unicorn/prefer-object-destructuring-defaults](https://lintradar.dev/rules/unicorn/prefer-object-destructuring-defaults.md): Prefer object destructuring defaults over default object literals with spread.
- [unicorn/prefer-https](https://lintradar.dev/rules/unicorn/prefer-https.md): Prefer HTTPS over HTTP.
- [unicorn/prefer-observer-apis](https://lintradar.dev/rules/unicorn/prefer-observer-apis.md): Prefer observer APIs over resize and scroll listeners with layout reads.

---

Web page: https://lintradar.dev/rules/unicorn/prefer-location-assign. Data from npm and the linters' documentation, collected Dec 4, 2026. Site guide for agents: https://lintradar.dev/llms.txt
