라이브러리

[JAVASCRIPT] str.concat(str2, str3, …) - 문자열을 결합하여 새 문자열 반환




concat() 메서드 소개


JavaScript의 String 객체에는 concat() 메서드가 있습니다. 이 메서드는 하나 이상의 문자열을 하나의 문자열로 합쳐서 반환합니다. concat() 메서드는 원본 문자열을 변경하지 않고 새로운 문자열을 반환합니다.

concat() 메서드의 사용법


concat() 메서드는 다음과 같은 형태로 사용할 수 있습니다.

#hostingforum.kr
javascript

str.concat(str2, str3, ...)



str은 원본 문자열이고, str2, str3, ...은 합쳐질 문자열입니다.

concat() 메서드의 예제


#hostingforum.kr
javascript

// 원본 문자열

let str = "Hello, ";



// 합쳐질 문자열

let str2 = "World!";

let str3 = " How are you?";



// concat() 메서드를 사용하여 문자열을 합침

let result = str.concat(str2, str3);



console.log(result); // Hello, World! How are you?



concat() 메서드의 반환값


concat() 메서드는 새로운 문자열을 반환합니다. 원본 문자열은 변경되지 않습니다.

#hostingforum.kr
javascript

let str = "Hello, ";

let str2 = "World!";



let result = str.concat(str2);

console.log(str); // Hello, (원본 문자열은 변경되지 않음)

console.log(result); // Hello, World!



concat() 메서드의 사용 예시


concat() 메서드는 여러 가지 사용 예시가 있습니다. 예를 들어, 다음과 같이 사용할 수 있습니다.

#hostingforum.kr
javascript

// 이름과 나이를 합쳐서 출력

let name = "John";

let age = 30;



let result = "My name is " + name + " and I am " + age + " years old.";

console.log(result); // My name is John and I am 30 years old.



// concat() 메서드를 사용하여 이름과 나이를 합침

result = "My name is " + name.concat(" ").concat(age).concat(" years old.");

console.log(result); // My name is John30 years old.



concat() 메서드의 결론


concat() 메서드는 하나 이상의 문자열을 하나의 문자열로 합쳐서 반환하는 메서드입니다. 원본 문자열은 변경되지 않으며, 새로운 문자열을 반환합니다. concat() 메서드는 여러 가지 사용 예시가 있으며, JavaScript 개발자에게 유용한 메서드입니다.
  • profile_image
    나우호스팅 @pcs8404 

    호스팅포럼 화이팅!

    댓글목록

    등록된 댓글이 없습니다.

  • 전체 10,077건 / 9 페이지

검색

게시물 검색