Sometimes, we may run into the ‘RangeError: repeat count must be less than infinity’ when we’re developing JavaScript apps.
In this article, we’ll look at how to fix the ‘RangeError: repeat count must be less than infinity’ when we’re developing JavaScript apps.
Fix the ‘RangeError: repeat count must be less than infinity’ When Developing JavaScript Apps
To fix the ‘RangeError: repeat count must be less than infinity’ when we’re developing JavaScript apps, we should make sure we call the String.prototype.repeat
method with an argument that’s a valid JavaScript integer.
For instance, instead of writing:
'abc'.repeat(Infinity);
'a'.repeat(2**28);
which either has Infinity
or an integer that’s bigger than the max for a JavaScript number, we write:
'abc'.repeat(0);
'abc'.repeat(1);
'abc'.repeat(2);
'abc'.repeat(3.5);
which all are called with valid numbers as arguments.
3.5 will be converted to an integer when repeat
is run.
Conclusion
To fix the ‘RangeError: repeat count must be less than infinity’ when we’re developing JavaScript apps, we should make sure we call the String.prototype.repeat
method with an argument that’s a valid JavaScript integer.