Nest all content into snippets
This commit is contained in:
27
snippets/js/s/validate-number.md
Normal file
27
snippets/js/s/validate-number.md
Normal file
@ -0,0 +1,27 @@
|
||||
---
|
||||
title: Validate number
|
||||
type: snippet
|
||||
language: javascript
|
||||
tags: [math]
|
||||
cover: flower-portrait-9
|
||||
dateModified: 2020-10-22T20:23:26+03:00
|
||||
---
|
||||
|
||||
Checks if the given value is a number.
|
||||
|
||||
- Use `parseFloat()` to try to convert `n` to a number.
|
||||
- Use `Number.isNaN()` and logical not (`!`) operator to check if `num` is a number.
|
||||
- Use `Number.isFinite()` to check if `num` is finite.
|
||||
- Use `Number` and the loose equality operator (`==`) to check if the coercion holds.
|
||||
|
||||
```js
|
||||
const validateNumber = n => {
|
||||
const num = parseFloat(n);
|
||||
return !Number.isNaN(num) && Number.isFinite(num) && Number(n) == n;
|
||||
}
|
||||
```
|
||||
|
||||
```js
|
||||
validateNumber('10'); // true
|
||||
validateNumber('a'); // false
|
||||
```
|
||||
Reference in New Issue
Block a user