|
| 1 | +--- |
| 2 | +Title: '.MIN_SAFE_INTEGER' |
| 3 | +Description: 'Represents the smallest safe integer in JavaScript, below which integer precision is not guaranteed.' |
| 4 | +Subjects: |
| 5 | + - 'Computer Science' |
| 6 | + - 'Web Development' |
| 7 | +Tags: |
| 8 | + - 'Integers' |
| 9 | + - 'JavaScript' |
| 10 | + - 'Numbers' |
| 11 | +CatalogContent: |
| 12 | + - 'introduction-to-javascript' |
| 13 | + - 'paths/front-end-engineer-career-path' |
| 14 | +--- |
| 15 | + |
| 16 | +In JavaScript, the **`.MIN_SAFE_INTEGER`** property is a static constant that represents the smallest safe integer in JavaScript: `-(2^53 - 1)` or `-9007199254740991`. JavaScript uses the IEEE-754 double-precision format for all numbers, meaning not all integers can be represented exactly. A safe integer is one that: |
| 17 | + |
| 18 | +- Can be precisely represented |
| 19 | +- Doesn’t lose precision during arithmetic |
| 20 | +- Can be accurately compared |
| 21 | + |
| 22 | +Since JavaScript is dynamically typed, all numbers fall under the `Number` type and are treated as floating-point values. `.MIN_SAFE_INTEGER` isn’t tied to any variable as it’s accessed directly via `.MIN_SAFE_INTEGER`. Values smaller than this limit may suffer from rounding errors or incorrect comparisons due to floating-point precision limits. |
| 23 | + |
| 24 | +## Syntax |
| 25 | + |
| 26 | +```pseudo |
| 27 | +Number.MIN_SAFE_INTEGER |
| 28 | +``` |
| 29 | + |
| 30 | +**Parameters:** |
| 31 | + |
| 32 | +`.MIN_SAFE_INTEGER` is a static property of the Number object. It's accessed directly without calling a function or passing arguments. |
| 33 | + |
| 34 | +**Return value:** |
| 35 | + |
| 36 | +It returns a number: `-9007199254740991`. This is the smallest integer JavaScript can represent safely without precision loss using the `Number` type (IEEE-754 double-precision). |
| 37 | + |
| 38 | +## Example |
| 39 | + |
| 40 | +In this example, subtracting beyond the minimum safe integer leads to precision loss, causing distinct values to compare as equal: |
| 41 | + |
| 42 | +```js |
| 43 | +console.log(Number.MIN_SAFE_INTEGER); |
| 44 | + |
| 45 | +const a = Number.MIN_SAFE_INTEGER - 1; |
| 46 | +const b = Number.MIN_SAFE_INTEGER - 2; |
| 47 | + |
| 48 | +console.log(a === b); |
| 49 | +``` |
| 50 | + |
| 51 | +The output of this code is: |
| 52 | + |
| 53 | +```shell |
| 54 | +-9007199254740991 |
| 55 | +true |
| 56 | +``` |
| 57 | + |
| 58 | +This demonstrates what happens when values go below the minimum safe integer. Although `a` and `b` should be distinct (`-9007199254740992` and `-9007199254740993`), both are stored as the same value due to floating-point precision limits. So, `a === b` returns `true`, even though they are not mathematically equal. |
| 59 | + |
| 60 | +## Codebyte Example |
| 61 | + |
| 62 | +In this codebyte example, `.isSafeInteger()` is used to check if a value just below the minimum safe integer is still considered safe: |
| 63 | + |
| 64 | +```codebyte/javascript |
| 65 | +console.log("Minimum safe integer in JavaScript:"); |
| 66 | +console.log(Number.MIN_SAFE_INTEGER); |
| 67 | +
|
| 68 | +const unsafeValue = Number.MIN_SAFE_INTEGER - 1; |
| 69 | +console.log("Unsafe value:", unsafeValue); |
| 70 | +
|
| 71 | +console.log("Is unsafeValue a safe integer?"); |
| 72 | +console.log(Number.isSafeInteger(unsafeValue)); |
| 73 | +``` |
0 commit comments