# string.prototype.trim

> ES5 spec-compliant shim for String.prototype.trim

Latest version **1.2.11** (published 2026-06-05) · MIT license · 0 weekly downloads

> **Native alternative:** String.prototype.trim — Use native JavaScript (Node 0.10.0+) (https://developer.mozilla.orgGlobal_Objects/String/trim)

## Install

```sh
npm install string.prototype.trim
pnpm add string.prototype.trim
yarn add string.prototype.trim
bun add string.prototype.trim
```

## Health

**Score 53/100 (C)** — status: active.

Positive: has types package; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

## Facts

| | |
|---|---|
| Version | 1.2.11 |
| Published | 2026-06-05 |
| First published | 2015-08-09 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | separate (@types/string.prototype.trim) |
| Module format | CommonJS |
| Node | >= 0.4 |
| Dependencies | 8 |
| Unpacked size | 36.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 13 |
| Author | Jordan Harband |
| Maintainers | ljharb |
| Keywords | String.prototype.trim, string, ES5, shim, trim, polyfill, es-shim API |

## Links

- npm: https://www.npmjs.com/package/string.prototype.trim
- Repository: https://github.com/es-shims/String.prototype.trim
- Homepage: https://github.com/es-shims/String.prototype.trim#readme
- Issues: https://github.com/es-shims/String.prototype.trim/issues
- Funding: https://github.com/sponsors/ljharb
- npm.io page: https://npm.io/package/string.prototype.trim

## Dependencies (8)

- [call-bind](https://npm.io/package/call-bind.md) ^1.0.9
- [call-bound](https://npm.io/package/call-bound.md) ^1.0.4
- [es-abstract](https://npm.io/package/es-abstract.md) ^1.24.2
- [es-object-atoms](https://npm.io/package/es-object-atoms.md) ^1.1.2
- [safe-regex-test](https://npm.io/package/safe-regex-test.md) ^1.1.0
- [define-properties](https://npm.io/package/define-properties.md) ^1.2.1
- [define-data-property](https://npm.io/package/define-data-property.md) ^1.1.4
- [has-property-descriptors](https://npm.io/package/has-property-descriptors.md) ^1.0.2

## Alternatives

- [@mce/gif](https://npm.io/package/@mce/gif.md) — 2.6K weekly downloads
- [cleanse](https://npm.io/package/cleanse.md) — 173 weekly downloads
- [str](https://npm.io/package/str.md) — 127 weekly downloads
- [naming](https://npm.io/package/naming.md) — 95 weekly downloads
- [tap-telco-api](https://npm.io/package/tap-telco-api.md) — 19 weekly downloads

## Recent versions

- 1.2.11 (latest) — 2026-06-05
- 1.2.10 — 2024-12-11
- 1.2.9 — 2024-03-17
- 1.2.8 — 2023-09-07
- 1.2.7 — 2022-11-07
- 1.2.6 — 2022-04-24
- 1.2.5 — 2021-10-04
- 1.2.4 — 2021-02-21
- 1.2.3 — 2020-11-21
- 1.2.2 — 2020-09-15
- 1.2.1 — 2019-12-16
- 1.2.0 — 2019-07-24
- 1.1.2 — 2016-02-06
- 1.1.1 — 2015-08-16
- 1.1.0 — 2015-08-16
- … 1 more at https://npm.io/package/string.prototype.trim/versions

## README

# String.prototype.trim <sup>[![Version Badge][npm-version-svg]][package-url]</sup>

[![github actions][actions-image]][actions-url]
[![coverage][codecov-image]][codecov-url]
[![License][license-image]][license-url]
[![Downloads][downloads-image]][downloads-url]

[![npm badge][npm-badge-png]][package-url]

An ES5 spec-compliant `String.prototype.trim` shim. Invoke its "shim" method to shim `String.prototype.trim` if it is unavailable.

This package implements the [es-shim API](https://github.com/es-shims/api) interface. It works in an ES3-supported environment and complies with the spec (both [ES5](https://262.ecma-international.org/5.1/#sec-15.5.4.20) and [current](https://tc39.es/ecma262/#sec-string.prototype.trim)).

Most common usage:

```js
var assert = require('assert');
var trim = require('string.prototype.trim');

assert(trim(' \t\na \t\n') === 'a');

trim.shim(); // will be a no-op if not needed

assert(trim(' \t\na \t\n') === ' \t\na \t\n'.trim());
```

## Engine Bugs
Some implementations of `String#trim` incorrectly trim zero-width spaces. This shim detects and corrects this behavior.

## Tests
Simply clone the repo, `npm install`, and run `npm test`

[package-url]: https://npmjs.com/package/string.prototype.trim
[npm-version-svg]: https://versionbadg.es/es-shims/String.prototype.trim.svg
[deps-svg]: https://david-dm.org/es-shims/String.prototype.trim.svg
[deps-url]: https://david-dm.org/es-shims/String.prototype.trim
[dev-deps-svg]: https://david-dm.org/es-shims/String.prototype.trim/dev-status.svg
[dev-deps-url]: https://david-dm.org/es-shims/String.prototype.trim#info=devDependencies
[license-image]: https://img.shields.io/npm/l/string.prototype.trim.svg
[license-url]: LICENSE
[downloads-image]: https://img.shields.io/npm/dm/string.prototype.trim.svg
[downloads-url]: https://npm-stat.com/charts.html?package=string.prototype.trim
[codecov-image]: https://codecov.io/gh/es-shims/String.prototype.trim/branch/main/graphs/badge.svg
[codecov-url]: https://app.codecov.io/gh/es-shims/String.prototype.trim/
[actions-image]: https://img.shields.io/github/check-runs/es-shims/String.prototype.trim/main
[actions-url]: https://github.com/es-shims/String.prototype.trim/actions

---
_Source: https://npm.io/package/string.prototype.trim · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
