How to use JavaScript String repeat()

String repeat() method

String repeat() method

Program to use JavaScript String repeat() method

let a = "Hello ";
    
    let b = a.repeat(3);
    
    console.log(b);
    

Output

Hello Hello Hello
    

Steps Description

  • Declare two variables a and b
  • Initialize two variable with value of string a = “Hello “.repeat(3) and b
  • repeat() Method Whatever words or letters are written in the string, they can be repeated again and again.
  • The value of b variable is displayed with the help of console.log inbuilt function

String repeat() method

Program to use JavaScript String repeat() method

let c = "Hello World \n";
    
    let d = c.repeat(3);
    
    console.log(d);
    

Output

Hello World 
    Hello World 
    Hello World 
    

Steps Description

  • Declare two variables c and d
  • Initialize two variable with value of string c = “Hello World \n”.repeat(3) and d
  • repeat() Method Whatever words or letters are written in the string, they can be repeated again and again.
  • The value of d variable is displayed with the help of console.log inbuilt function