Skip to content

Commit 8d0681f

Browse files
authored
[Term Entry] JavaScript Number Methods: .isSafeInteger()
* Add .isSafeInteger() term entry for JavaScript number methods * Add .isSafeInteger() term entry for JavaScript number methods * Update isSafeInteger.md * Update yarn.lock * Update isSafeInteger.md * Update isSafeInteger.md ---------
1 parent 66f1808 commit 8d0681f

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
---
2+
Title: '.isSafeInteger()'
3+
Description: 'Checks whether the passed value is a safe integer.'
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 **`.isSafeInteger()`** method is a static method of the `Number` object in JavaScript which determines whether the provided value is a safe integer: an integer that can be exactly represented using the IEEE-754 double-precision format.
16+
17+
A safe integer satisfies:
18+
19+
$$
20+
-(2^{53} - 1) \leq \text{value} \leq 2^{53} - 1
21+
$$
22+
23+
If the condition holds, the method returns `true`, otherwise, it returns `false`.
24+
25+
## Syntax
26+
27+
```pseudo
28+
Number.isSafeInteger(value);
29+
```
30+
31+
**Parameters:**
32+
33+
- `value`: The value to be tested for being a safe integer.
34+
35+
**Return value:**
36+
37+
Returns `true` if the given `value` is of type `number`, is an integer, and is within the safe integer range (`-(2^53 - 1) to 2^53 - 1`). Otherwise, it returns `false`.
38+
39+
## Example
40+
41+
This example demonstrates how `.isSafeInteger()` returns `true` for integers and `false` for non-integers or values outside the safe range:
42+
43+
```js
44+
console.log(Number.isSafeInteger(10));
45+
console.log(Number.isSafeInteger(3.14));
46+
console.log(Number.isSafeInteger(Math.pow(2, 53)));
47+
console.log(Number.isSafeInteger(2 ** 53 - 1));
48+
```
49+
50+
The output of this code is:
51+
52+
```shell
53+
true
54+
false
55+
false
56+
true
57+
```
58+
59+
## Codebyte Example
60+
61+
This codebyte tests various values to show how the method handles whole numbers, decimals, and edge cases at the limits of JavaScript’s safe integer range:
62+
63+
```codebyte/javascript
64+
console.log(Number.isSafeInteger(100));
65+
console.log(Number.isSafeInteger(3.5));
66+
console.log(Number.isSafeInteger(2 ** 53 - 1));
67+
console.log(Number.isSafeInteger(2 ** 53));
68+
```

0 commit comments

Comments
 (0)