Skip to content

Commit bb79a4f

Browse files
authored
[Term Entry] JavaScript Number Methods: .MAX_SAFE_INTEGER
* [Term Entry] JavaScript Number methods: .MAX_SAFE_INTEGER (#7314) * Update MAX-SAFE-INTEGER.md * Update MAX-SAFE-INTEGER.md * Update MAX-SAFE-INTEGER.md ---------
1 parent 8d0681f commit bb79a4f

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
---
2+
Title: '.MAX_SAFE_INTEGER'
3+
Description: 'Represents the largest integer that JavaScript can use while maintaining exact precision.'
4+
Subjects:
5+
- 'Computer Science'
6+
- 'Web Development'
7+
Tags:
8+
- 'Methods'
9+
- 'Numbers'
10+
CatalogContent:
11+
- 'introduction-to-javascript'
12+
- 'paths/front-end-engineer-career-path'
13+
---
14+
15+
The **`.MAX_SAFE_INTEGER`** static data property represents the maximum safe integer in JavaScript, which is:
16+
17+
$$
18+
2^{53} - 1 = 9007199254740991
19+
$$
20+
21+
It’s the largest integer that can be safely represented using the `Number` type without losing precision. This property is part of the `Number` class. To check if a value is a safe integer, use the `.isSafeInteger()` method. For integers larger than this limit, consider using the `BigInt` type instead.
22+
23+
## Syntax
24+
25+
```pseudo
26+
Number.MAX_SAFE_INTEGER;
27+
```
28+
29+
**Parameters:**
30+
31+
None as it's a static data property, not a function.
32+
33+
**Return value:**
34+
35+
Returns the largest safe integer representable by JavaScript's `Number` type.
36+
37+
## Example
38+
39+
This example demonstrates how `.MAX_SAFE_INTEGER` defines the upper limit for safe integers in JavaScript:
40+
41+
```js
42+
console.log(Number.MAX_SAFE_INTEGER);
43+
44+
console.log(Number.isSafeInteger(Number.MAX_SAFE_INTEGER));
45+
console.log(Number.isSafeInteger(Number.MAX_SAFE_INTEGER + 1));
46+
```
47+
48+
The output produced by this code is:
49+
50+
```shell
51+
9007199254740991
52+
true
53+
false
54+
```
55+
56+
## Codebyte Example
57+
58+
A codebyte example showing why it's best to avoid numbers larger than `.MAX_SAFE_INTEGER`:
59+
60+
```codebyte/javascript
61+
const a = Number.MAX_SAFE_INTEGER + 1;
62+
const b = Number.MAX_SAFE_INTEGER + 2;
63+
console.log(a === b);
64+
```
65+
66+
Both `a` and `b` evaluate to the same value because they exceed the maximum safe integer JavaScript can represent accurately. When numbers go beyond this limit, JavaScript starts to lose precision, which can lead to incorrect comparisons and unexpected results.

0 commit comments

Comments
 (0)