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.
String
값의 trim()
메서드는 문자열 양 끝의 공백을 제거하면서 원본 문자열을 수정하지 않고 새로운 문자열을 반환합니다.
한쪽 끝의 공백만 제거한 새로운 문자열을 반환하려면 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.