Categories
JavaScript Answers

How to Fix the ‘RangeError: radix must be an integer’ Error in Our JavaScript App?

Sometimes, we may run into the ‘RangeError: radix must be an integer’ when we’re developing JavaScript apps.

In this article, we’ll look at how to fix the ‘RangeError: radix must be an integer’ when we’re developing JavaScript apps.

Fix the ‘RangeError: radix must be an integerRangeError: precision is out of range’ When Developing JavaScript Apps

To fix the ‘RangeError: radix must be an integer’ when we’re developing JavaScript apps, we should make sure we pass in an integer between 2 and 36 inclusive as the value of the optional radix parameter of the Number.prototype.toString or BigInt.prototype.toString methods.

In Edge, the same error is called RangeError: invalid argument.

In Firefox, the error is called RangeError: invalid argument.

And in Chrome, the same error is called RangeError: toString() radix argument must be between 2 and 36.

Therefore, instead of writing the following:

(12).toString(0);
(12).toString(1);
(12).toString(37);
(12).toString(150);
(12071989).toString('MM-dd-yyyy')

We write:

(12).toString(2);  
(13).toString(8);  
(0x42).toString(10);
(100000).toString(16)

Conclusion

To fix the ‘RangeError: radix must be an integer’ when we’re developing JavaScript apps, we should make sure we pass in an integer between 2 and 36 inclusive as the value of the optional radix parameter of the Number.prototype.toString or BigInt.prototype.toString methods.

In Edge, the same error is called RangeError: invalid argument.

In Firefox, the error is called RangeError: invalid argument.

And in Chrome, the same error is called RangeError: toString() radix argument must be between 2 and 36.

Categories
JavaScript Answers

How to Fix the ‘RangeError: precision is out of range’ Error in Our JavaScript App?

Sometimes, we may run into the ‘RangeError: precision is out of range’ when we’re developing JavaScript apps.

In this article, we’ll look at how to fix the ‘RangeError: precision is out of range’ when we’re developing JavaScript apps.

Fix the ‘RangeError: precision is out of range’ When Developing JavaScript Apps

To fix the ‘RangeError: precision is out of range’ when we’re developing JavaScript apps, we should make sure we pass in a number between 0 and 20 into the toExponential, toFixed or toPrecision methods.

In Edge, the equivalent error is RangeError: The number of fractional digits is out of range or RangeError: The precision is out of range.

In Firefox, the equivalent error is RangeError: precision {0} out of range.

In Chrome, the equivalent error is RangeError: toExponential() argument must be between 0 and 20, RangeError: toFixed() digits argument must be between 0 and 20, or RangeError: toPrecision() argument must be between 1 and 21.

For instance, we shouldn’t write code like:

88.toExponential(-1); 
88.1234.toExponential(101); 

2.34.toFixed(-100);    
2.34.toFixed(1001);        

1234.5.toPrecision(-1);    
1234.5.toPrecision(101);

Instead, we write:

88.toExponential(11); 
88.1234.toExponential(11); 

2.34.toFixed(10);    
2.34.toFixed(10);        

1234.5.toPrecision(1);    
1234.5.toPrecision(11);

Conclusion

To fix the ‘RangeError: precision is out of range’ when we’re developing JavaScript apps, we should make sure we pass in a number between 0 and 20 into the toExponential, toFixed or toPrecision methods.

In Edge, the equivalent error is RangeError: The number of fractional digits is out of range or RangeError: The precision is out of range.

In Firefox, the equivalent error is RangeError: precision {0} out of range.

In Chrome, the equivalent error is RangeError: toExponential() argument must be between 0 and 20, RangeError: toFixed() digits argument must be between 0 and 20, or RangeError: toPrecision() argument must be between 1 and 21.

Categories
JavaScript Answers

How to Fix the ‘RangeError: invalid date’ Error in Our JavaScript App?

Sometimes, we may run into the ‘RangeError: invalid date’ when we’re developing JavaScript apps.

In this article, we’ll look at how to fix the ‘RangeError: invalid date’ when we’re developing JavaScript apps.

Fix the ‘RangeError: invalid date’ When Developing JavaScript Apps

To fix the ‘RangeError: invalid date’ when we’re developing JavaScript apps, we should make sure that we pass in a valid date string value into the Date constructor or the Date.parse method.

In Edge, the error message for the same error is RangeError: invalid date.

In Chrome, the error message for the same error is RangeError: invalid time value or RangeError: Provided date is not in valid range.

For instance, we should write code like:

new Date('foo-bar 2014');
new Date('2024-25-23').toISOString();
new Date('foo-bar 2014').toString();

since the arguments are all invalid date strings.

Instead, we should write:

new Date('05 October 2021 14:48 UTC');
new Date(1317826080);

Conclusion

To fix the ‘RangeError: invalid date’ when we’re developing JavaScript apps, we should make sure that we pass in a valid date string value into the Date constructor or the Date.parse method.

In Edge, the error message for the same error is RangeError: invalid date.

In Chrome, the error message for the same error is RangeError: invalid time value or RangeError: Provided date is not in valid range.

Categories
JavaScript Answers

How to Fix the ‘RangeError: invalid array length’ Error in Our JavaScript App?

Sometimes, we may run into the ‘RangeError: invalid array length’ when we’re developing JavaScript apps.

In this article, we’ll look at how to fix the ‘RangeError: invalid array length’ when we’re developing JavaScript apps.

Fix the ‘RangeError: invalid array length’ When Developing JavaScript Apps

To fix the ‘RangeError: invalid array length’ when we’re developing JavaScript apps, we should make sure the array length value that we call the Array and ArrayBuffer constructors are called with a valid value.

Also, we should make sure we set the length property of an array to a non-negative integer.

For instance, these are all code with invalid length value passed into the Array and ArrayBuffer constructors:

new Array(Math.pow(2, 40))
new Array(-1)
new ArrayBuffer(Math.pow(2, 32)) 
new ArrayBuffer(-1)

We also can’t set the length property of an array to an invalid number:

let a = [];
a.length = - 1; 

To fix the error, we can write code like:

new Array(100)
new ArrayBuffer(100) 

let a = [];
a.length = - 1; 

Conclusion

To fix the ‘RangeError: invalid array length’ when we’re developing JavaScript apps, we should make sure the array length value that we call the Array and ArrayBuffer constructors are called with a valid value.

Also, we should make sure we set the length property of an array to a non-negative integer.

Categories
JavaScript Answers

How to Fix the ‘RangeError: argument is not a valid code point ‘ Error in Our JavaScript App?

Sometimes, we may run into the ‘RangeError: argument is not a valid code point’ when we’re developing JavaScript apps.

In this article, we’ll look at how to fix the ‘RangeError: argument is not a valid code point’ when we’re developing JavaScript apps.

Fix the ‘RangeError: argument is not a valid code point’ When Developing JavaScript Apps

To fix the ‘RangeError: argument is not a valid code point’ when we’re developing JavaScript apps, we should make sure that we call the String.fromCodePoint method with a valid code point value.

For instance, the following will throw this error:

String.fromCodePoint('_');      
String.fromCodePoint(Infinity); 
String.fromCodePoint(-1);       
String.fromCodePoint(3.14);     
String.fromCodePoint(3e-2);     
String.fromCodePoint(NaN);

These values aren’t numbers that represent characters in the Unicode character set, so the calls above will all throw errors.

On the other hand, the following won’t throw the error since the arguments are all valid Unicode code point values:

String.fromCodePoint(42);     
String.fromCodePoint(65, 90); 
String.fromCodePoint(0x404);  
String.fromCodePoint(0x2F804);
String.fromCodePoint(194564);
String.fromCodePoint(0x1D306, 0x61, 0x1D307)

Conclusion

To fix the ‘RangeError: argument is not a valid code point’ when we’re developing JavaScript apps, we should make sure that we call the String.fromCodePoint method with a valid code point value.