String.prototype.repeat()

- repeat() 메서드는 문자열을 주어진 횟수만큼 반복해 붙인 새로운 문자열을 반환합니다.

구문

str.repeat(count);

- count

문자열을 반복할 횟수. 0과 양의 무한대 사이의 정수([0, +∞)).

-

반환값

- 현재 문자열을 주어진 횟수만큼 반복해 붙인 새로운 문자열.

예제

'abc'.repeat(-1);   // RangeError
'abc'.repeat(0);    // ''
'abc'.repeat(1);    // 'abc'
'abc'.repeat(2);    // 'abcabc'
'abc'.repeat(3.5);  // 'abcabcabc' (count will be converted to integer)
'abc'.repeat(1/0);  // RangeError

({ toString: () => 'abc', repeat: String.prototype.repeat }).repeat(2);
// 'abcabc' (repeat() is a generic method)

 

+ Recent posts