Converts a value to a safe integer.
Math.max()
and Math.min()
to find the closest safe value.Math.round()
to convert to an integer.const toSafeInteger = num =>
Math.round(
Math.max(Math.min(num, Number.MAX_SAFE_INTEGER), Number.MIN_SAFE_INTEGER)
);
toSafeInteger('3.2'); // 3
toSafeInteger(Infinity); // 9007199254740991
Would you like to help us improve 30 seconds of code?Take a quick survey
JavaScript, Math
Converts an integer to its roman numeral representation.
Accepts value between 1
and 3999
(both inclusive).
JavaScript, Math
Creates an array of numbers in the arithmetic progression, starting with the given positive integer and up to the specified limit.
JavaScript, Math
Converts a number to an array of digits, removing its sign if necessary.