Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
129
rated 0 times [  133] [ 4]  / answers: 1 / hits: 16029  / 13 Years ago, wed, june 22, 2011, 12:00:00

I'm doing some trouble-shooting and want to add a check that a parameter to a function is a number. How do I do this?



Something like this...



function fn(id) {
return // true iff id is a number else false
}


Even better is if I can check that the parameter is a number AND a valid integer.


More From » javascript

 Answers
21
function fn(id) {
return typeof(id) === 'number';
}


To also check if it’s an integer:



function fn(id) {
return typeof(id) === 'number' &&
isFinite(id) &&
Math.round(id) === id;
}

[#91565] Tuesday, June 21, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
pierre

Total Points: 716
Total Questions: 128
Total Answers: 102

Location: Djibouti
Member since Sun, Feb 27, 2022
2 Years ago
;