You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 1-js/05-data-types/02-number/article.md
+32-1Lines changed: 32 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -334,6 +334,35 @@ alert( isFinite(num) );
334
334
335
335
Please note that an empty or a space-only string is treated as `0` in all numeric functions including `isFinite`.
336
336
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
+
337
366
```smart header="Compare with `Object.is`"
338
367
There is a special built-in method `Object.is` that compares values like `===`, but is more reliable for two edge cases:
339
368
@@ -435,7 +464,9 @@ For different numeral systems:
435
464
For regular number tests:
436
465
437
466
- `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`
439
470
440
471
For converting values like `12pt` and `100px` to a number:
0 commit comments