concat() 메서드는 매개변수로 전달된 모든 문자열을 호출 문자열에 붙인 새로운 문자열을 반환합니다.
구문
str.concat(string2, string3[, ..., stringN])
매개변수
string2...stringN- 합칠 문자열.
반환값
주어진 문자열을 모두 붙인 새로운 문자열.
설명
concat() 함수는 호출 문자열에 매개변수 문자열을 이어 붙인 결과를 반환합니다. 원본 문자열과 결과 문자열의 변형은 서로에게 영향을 미치지 않습니다. 매개변수의 값이 문자열이 아니면 계산 전에 문자열로 변환합니다.
예제
concat() 사용하기
아래 예제에서는 문자열을 결합하여 새로운 문자열을 만듭니다.
var hello = 'Hello, ';
console.log(hello.concat('Kevin', '. Have a nice day.'));
/* Hello, Kevin. Have a nice day. */
var greetList = ['Hello', ' ', 'Venkat', '!'];
"".concat(...greetList); // "Hello Venkat!"
"".concat({}); // [object Object]
"".concat([]); // ""
"".concat(null); // "null"
"".concat(true); // "true"
"".concat(4, 5); // "45"
성능
concat() 메서드보다 할당 연산자 (+, +=)를 사용하는게 더 좋습니다. 성능 테스트 결과에 따르면 할당 연산자의 속도가 몇 배 빠릅니다.
명세
| Specification | Status | Comment |
|---|---|---|
| ECMAScript 3rd Edition (ECMA-262) | Standard | Initial definition. Implemented in JavaScript 1.2. |
| ECMAScript 5.1 (ECMA-262) The definition of 'String.prototype.concat' in that specification. |
Standard | |
| ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'String.prototype.concat' in that specification. |
Standard | |
| ECMAScript Latest Draft (ECMA-262) The definition of 'String.prototype.concat' in that specification. |
Draft |
브라우저 호환성
Update compatibility data on GitHub
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
concat | Chrome Full support Yes | Edge Full support 12 | Firefox Full support 1 | IE Full support Yes | Opera Full support Yes | Safari Full support Yes | WebView Android Full support Yes | Chrome Android Full support Yes | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support Yes | nodejs Full support Yes |
Legend
- Full support
- Full support