--- title: 'RangeError: repeat count must be non-negative' slug: Web/JavaScript/Reference/Errors/Negative_repetition_count tags: - Errors - JavaScript - RangeError - 错误 translation_of: Web/JavaScript/Reference/Errors/Negative_repetition_count ---
RangeError: repeat count must be non-negative (Firefox) RangeError: Invalid count value (Chrome)
{{jsxref("RangeError")}}
代码中使用了 {{jsxref("String.prototype.repeat()")}}方法。它有一个计数参数,表示重复该字符串的次数。该参数必须在 0 及正 {{jsxref("Infinity")}} 之间,且不能为负数。该值的合法范围可以这样表示: [0, +∞)。
'abc'.repeat(-1); // RangeError
'abc'.repeat(0); // '' 'abc'.repeat(1); // 'abc' 'abc'.repeat(2); // 'abcabc' 'abc'.repeat(3.5); // 'abcabcabc' (count will be converted to integer)