Javascript String repeat()

Introduction

Javascript repeat() method accepts a single integer argument count.

str.repeat(count)
  • count - the number of times to repeat the string.

It copies the string count times, and concatenates all the copies.

let stringValue = "hi "; 
console.log(stringValue.repeat(16) + "java2s.com"); 

Testing the count parameter:

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



PreviousNext

Related