Skip to content

Commit 3bfb6cd

Browse files
authored
Merge pull request #3125 from chilipenko/patch-2
Add Number.isNaN and Number.isFinite
2 parents 962fa79 + 7df4cbb commit 3bfb6cd

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

1-js/05-data-types/02-number/article.md

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,35 @@ alert( isFinite(num) );
334334

335335
Please note that an empty or a space-only string is treated as `0` in all numeric functions including `isFinite`.
336336

337+
````smart header="`Number.isNaN` and `Number.isFinite`"
338+
[Number.isNaN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isNaN) and [Number.isFinite](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isFinite) methods are the more "strict" versions of `isNaN` and `isFinite` functions. They do not autoconvert their argument into a number, but check if it belongs to the `number` type instead.
339+
340+
- `Number.isNaN(value)` returns `true` if the argument belongs to the `number` type and it is `NaN`. In any other case it returns `false`.
341+
342+
```js run
343+
alert( Number.isNaN(NaN) ); // true
344+
alert( Number.isNaN("str" / 2) ); // true
345+
346+
// Note the difference:
347+
alert( Number.isNaN("str") ); // false, because "str" belongs to the string type, not the number type
348+
alert( isNaN("str") ); // true, because isNaN converts string "str" into a number and gets NaN as a result of this conversion
349+
```
350+
351+
- `Number.isFinite(value)` returns `true` if the argument belongs to the `number` type and it is not `NaN/Infinity/-Infinity`. In any other case it returns `false`.
352+
353+
```js run
354+
alert( Number.isFinite(123) ); // true
355+
alert( Number.isFinite(Infinity) ); //false
356+
alert( Number.isFinite(2 / 0) ); // false
357+
358+
// Note the difference:
359+
alert( Number.isFinite("123") ); // false, because "123" belongs to the string type, not the number type
360+
alert( isFinite("123") ); // true, because isFinite converts string "123" into a number 123
361+
```
362+
363+
Do not consider `Number.isNaN` and `Number.isFinite` methods as the more "correct" versions of `isNaN` and `isFinite` functions. They complement each other and are equally essential for different tasks.
364+
````
365+
337366
```smart header="Compare with `Object.is`"
338367
There is a special built-in method `Object.is` that compares values like `===`, but is more reliable for two edge cases:
339368

@@ -435,7 +464,9 @@ For different numeral systems:
435464
For regular number tests:
436465

437466
- `isNaN(value)` converts its argument to a number and then tests it for being `NaN`
438-
- `isFinite(value)` converts its argument to a number and returns `true` if it's a regular number, not `NaN/Infinity/-Infinity`
467+
- `Number.isNaN(value)` checks whether its argument belongs to the `number` type, and if so, tests it for being `NaN`
468+
- `isFinite(value)` converts its argument to a number and then tests it for not being `NaN/Infinity/-Infinity`
469+
- `Number.isFinite(value)` checks whether its argument belongs to the `number` type, and if so, tests it for not being `NaN/Infinity/-Infinity`
439470

440471
For converting values like `12pt` and `100px` to a number:
441472

0 commit comments

Comments
 (0)