Javascript Data Type How to - Create an N-times-a-character string








Question

We would like to know how to create an N-times-a-character string.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'>
<!--from   www  . ja  v  a 2 s.c  om-->
String.prototype.repeat = function (len) {
    return (new Array(len + 1)).join(this);
}
document.writeln('-/'.repeat(5));
document.writeln('<br/>');
document.writeln('a'.repeat(5));
</script>
</head>
<body>
</body>
</html>

The code above is rendered as follows: