JavaScript: Return true if the given value is a number, false otherwise
JavaScript fundamental (ES6 Syntax): Exercise-105 with Solution
Write a JavaScript program that returns true if the given value is a number, false otherwise.
- Use parseFloat() to try to convert n to a number.
- Use !Number.isNaN() 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.
Sample Solution:
JavaScript Code:
//#Source https://bit.ly/2neWfJ2
const validateNumber = n => !isNaN(parseFloat(n)) && isFinite(n) && Number(n) == n;
console.log(validateNumber('200'));
console.log(validateNumber('10'));
console.log(validateNumber('abc'));
Sample Output:
true true false
Pictorial Presentation:
Flowchart:

Live Demo:
See the Pen javascript-basic-exercise-105-1 by w3resource (@w3resource) on CodePen.
Improve this sample solution and post your code through Disqus
Previous: Write a JavaScript program to test a value, x, against a predicate function. If true, return fn(x). Else, return x.
Next: Write a JavaScript program to create an array of elements, ungrouping the elements in an array produced by zip and applying the provided function.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends
- Python Interview Questions and Answers: Comprehensive Guide
- Scala Exercises, Practice, Solution
- Kotlin Exercises practice with solution
- MongoDB Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - JOINS
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join