# prefer-url-can-parse

Rule `unicorn/prefer-url-can-parse`. Prefer `URL.canParse()` over constructing a `URL` in a try/catch for validation.

- 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: #1004 by reach, #1259 by growth

## Linters

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

## Code examples

### Incorrect: the rule reports this

```js
function canParseUrl(value) {
  try {
    new URL(value);
    return true;
  } catch {
    return false;
  }
}
```

```js
let valid;
try {
  new URL(value, base);
  valid = true;
} catch {
  valid = false;
}
```

### Correct: the rule accepts this

```js
function canParseUrl(value) {
  return URL.canParse(value);
}
```

```js
let valid;
valid = URL.canParse(value, base);
```

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-url-can-parse": "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% |
| [Unicorn unopinionated](https://lintradar.dev/presets/unicorn-unopinionated.md) | ESLint | `eslint-plugin-unicorn` | 236 | 9.5M | +30.3% |
| [XO](https://lintradar.dev/presets/xo.md) | ESLint | `eslint-config-xo` | 618 | 413K | −5.0% |

## Related rules

- [unicorn/prefer-unicode-code-point-escapes](https://lintradar.dev/rules/unicorn/prefer-unicode-code-point-escapes.md): Prefer Unicode code point escapes over legacy escape sequences.
- [unicorn/prefer-url-href](https://lintradar.dev/rules/unicorn/prefer-url-href.md): Prefer `URL#href` over stringifying a `URL`.
- [unicorn/prefer-toggle-attribute](https://lintradar.dev/rules/unicorn/prefer-toggle-attribute.md): Prefer using `Element#toggleAttribute()` to toggle attributes.
- [unicorn/prefer-url-search-parameters](https://lintradar.dev/rules/unicorn/prefer-url-search-parameters.md): Prefer `URLSearchParams` over manually splitting query strings.
- [unicorn/prefer-temporal-conversion](https://lintradar.dev/rules/unicorn/prefer-temporal-conversion.md): Prefer direct Temporal conversion methods.
- [unicorn/require-css-escape](https://lintradar.dev/rules/unicorn/require-css-escape.md): Require `CSS.escape()` for interpolated values in CSS selectors.

---

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