check if input type is number code example

Example 1: check if input is a number javascript

// Another option is typeof which return a string
if (typeof(val) === 'number') {
	// Guess what, it's a bloody number!
}

Example 2: javascript check if 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('   '); // false

Example 3: javascript check if variable is number

function isNumber(n) {
  return !isNaN(parseFloat(n)) && !isNaN(n - 0);
}