JavaScript Code

JavaScript String repeat()


String repeat()

str.repeat(count)
  • str is a string
  • repeat is function name
  • count is an integer

repeat() method repeats the string str by count number of times, and returns the resulting string. repeat() does not modify the original string str

Examples

1. repeat the string

let str = 'apple';
let output = str.repeat(4);
console.log('Output : ' + output);

2. if n is 0, then repeat() returns an empty string

let str = 'apple';
let output = str.repeat(0);
console.log('Output : ' + output);

3. if n is a negative value, then repeat() raises RangeError

let str = 'apple';
let output = str.repeat(-3);
console.log('Output : ' + output);

Copyright @2022