JavaScript Integer Maximum and Minimum Values Are Defined by Number.MAX_SAFE_INTEGER and Number.MIN_SAFE_INTEGER

Tadashi Shigeoka ·  Tue, July 24, 2018

In JavaScript, the maximum and minimum integer values can be obtained with Number.MAX_SAFE_INTEGER and Number.MIN_SAFE_INTEGER respectively.

JavaScript

Background

I needed to define the minimum integer value in a JSON file, and thought “What was JavaScript’s minimum integer value again?” so I researched it.

Maximum Integer Value Number.MAX_SAFE_INTEGER

JavaScript’s maximum integer value is defined by the constant Number.MAX_SAFE_INTEGER, and its value is 9007199254740991.

Minimum Integer Value Number.MIN_SAFE_INTEGER

JavaScript’s minimum integer value is defined by the constant Number.MIN_SAFE_INTEGER, and its value is -9007199254740991.

(Aside) JavaScript's Floating-Point Type Conforms to IEEE 754

It seems that JavaScript integer precision is maintained up to 2^53, not 2^64.

This appears to be because JavaScript has no integer type and everything is represented as floating-point type. JavaScript’s floating-point type is thought to conform to IEEE 754. (「Javascript の整数の精度が保たれるのは2の64乗ではなく、2の53乗までらしい。これは、JavaScript には、整数型がなく全て浮動小数点型で表現されるためのようです。JavaScript の浮動小数点型はIEEE 754 に準拠していると思われます。」)

JavaScriptの整数の精度が保たれるのは9007199254740991まで - Qiita

That’s all from the Gemba where I want to safely handle maximum and minimum integer values in JavaScript.