javascript check if variable is number
// The first 2 Variations return a Boolean
// They just work the opposite way around
// IsInteger
if (Number.isInteger(val)) {
// It is indeed a number
}
// isNaN (is not a number)
if (isNaN(val)) {
// It is not a number
}
// Another option is typeof which return a string
if (typeof(val) === 'number') {
// Guess what, it's a bloody number!
}function isNumber(n) { return /^-?[\d.]+(?:e-?\d+)?$/.test(n); }
------------------------
isNumber('123'); // true
isNumber('123abc'); // false
isNumber(5); // true
isNumber('q345'); // false
isNumber(null); // false
isNumber(undefined); // false
isNumber(false); // false
isNumber(' '); // falsefunction isNumber(n) {
return !isNaN(parseFloat(n)) && !isNaN(n - 0);
}
Also in JavaScript:
- bot prefix discord.js
- javascript get call stack
- await inside map js
- js resize div with mouse
- cordova js how to load local file in subdir
- dynamic page in html
- javascript push is not a function
- js go to see history
- mongoose find multiple conditions
- filter advantages in js
- javascript findindex
- js math random
- redblobgames pathfinding
- jquery select element with two classes
- console log larger
- uncaught TypeError: $.jajax is not a function
- navigation.openDrawer is not a function react native
- react transition group
- how to restart elasticsearch node
- vuejs send required props to dynamic component
- cnpj pattern js
- set background color dynamically javascript
- webpack css not shoud be empty
- redux devtools with middleware