js create string of length code example

Example 1: js string length

let str = "foo";
console.log(str.length);
// 3

Example 2: length of string in javascript

var str = "Hello World!";
var n = str.length;

Example 3: repeat js

repeatStr = (n, s) => s.repeat(n);

Example 4: string repeat

// hey buddy, here are different implementations, choose your favourite !!


// best implementation
repeatStr = (n, s) => s.repeat(n);


// short
function repeatStr(n, s) {
  return s.repeat(n);
}


// without using repeat
function repeatStr(n, s) {
  var str = "";
  for (var i = 0; i < n; i++) str += s;
  return str;
}

Example 5: javascript create string of given length

var str = new Array(len + 1).join( character );