String.prototype.trim()
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
trim()
は String
値のメソッドで、この文字列の両端からホワイトスペースを取り除き、元の文字列を変更せずに新しい文字列を返します。
一方の端からのみホワイトスペースを取り除いた新しい文字列を返したい場合は、trimStart()
または trimEnd()
を使用してください。
試してみましょう
const greeting = " Hello world! ";
console.log(greeting);
// Expected output: " Hello world! ";
console.log(greeting.trim());
// Expected output: "Hello world!";
構文
js
trim()
引数
なし。
返値
例
trim() の使用
次の例は、str
の両端からホワイトペースを取り除きます。
js
const str = " foo ";
console.log(str.trim()); // 'foo'
仕様書
Specification |
---|
ECMAScript® 2025 Language Specification # sec-string.prototype.trim |
ブラウザーの互換性
Report problems with this compatibility data on GitHubdesktop | mobile | server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
trim |
Legend
Tip: you can click/tap on a cell for more information.
- Full support
- Full support
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.