Sometimes, we may run into the ‘RangeError: repeat count must be non-negative’ when we’re developing JavaScript apps.
In this article, we’ll look at how to fix the ‘RangeError: repeat count must be non-negative’ when we’re developing JavaScript apps.
Fix the ‘RangeError: repeat count must be non-negative’ When Developing JavaScript Apps
To fix the ‘RangeError: repeat count must be non-negative’ 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(-100);
'a'.repeat(-28);
which has negative numbers as arguments, 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 non-negative’ 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.