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
Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️