JavaScript String method: repeat()
[this page | pdf | back links]
The repeat() method (when applied to a JavaScript
string) returns
a string that repeats a specified string a specified number of times.
It
has the following syntax with the following parameters:
string.repeat(n)
Parameter
|
Required / Optional
|
Description
|
n
|
Required
|
Number of times string
is repeated
|
EXAMPLE:
HTML USED IN THIS EXAMPLE:
<!DOCTYPE html>
<html> <!-- Copyright (c) Nematrian Limited 2018 -->
<head>
<style>
table,th,tr,td {border: 1px solid black; border-collapse: collapse;}
</style>
</head>
<body>
<table>
<tr>
<th>Example</th>
<th>Resulting value of <code>x</code></th>
</tr>
<tr>
<td><code id="Example"></code></td>
<td><code id="Result"></code></td>
</tr>
</table>
<script>
var z = "fg";
var n = 3;
document.getElementById("Example").innerHTML =
'var z = "' + z + '";<br>' +
'var x = z.repeat(' + n + ');';
document.getElementById("Result").innerHTML = z.repeat(n);
</script>
</body>
</html>
|
FUNCTION THAT MAY ASSIST IN TESTING WHETHER FEATURE IS SUPPORTED:
function isSupportedJavaScriptMethodStringRepeat() {
var z = "abc"; return !!z.repeat;
} |
NAVIGATION LINKS
Contents | Prev | Next | JavaScript String Variables