JavaScript Check Value is a numberWe will understand the JavaScript Check Value is a number in this article. There are various methods that can used to check if a value is a number. The methods are given below:
We will discuss each method one by one properly. We will discuss each method one by one properly. Utilizing the typeof() operatorThe typeof() operator returns the type of the variable. We will check whether the variable is of number type or not with the help of the typeof() operator. Demo 1: We will check if a variable is a number or not using the typeof() operator. Code: Output: Here is the output in which we can witness that the first value is a number and the second value is not a number. Demo 2: We will see another demo to check if a variable is a number or not using the typeof() operator. Code: Output: Here is the output in which we can witness that the first value is a number and the second value and the third value are not numbers. Utilizing the isNaN() methodThe NaN stands for not a number. The isNaN() method is utilized to return true if the value is not a number and returns false if the value is a number. We will put the not (!) sign in front of the method like this: "!isNaN()". The !isNaN() method will give opposite output which means if the value is not a number, then the !isNaN() method returns a Boolean value "true". If the value is a number, then !isNaN() method returns a Boolean value "false". Demo 1: We will check if a value is a number or not by utilizing the isNaN() method. Code: Output: We can witness in the output that the first value is a number but the second and the third value are not numbers. Demo 2: We will see another demo of checking whether a value is a number or not by utilizing the isNaN() method. Code: Output: Here is the output in which we can witness that the first value is a number but the second value & the third value are not numbers. Utilizing the Number.isFinite() methodThe Number.isFinite() method is utilized to check whether the variable value is numeric or not. If the value is numeric then it checks whether the value is finite or infinite. If the value is numeric and finite then it returns true. Demo 1: We will check if a value is a number or not utilizing the Number.isFinite() method with the try-catch statement. Code: Output: We can witness that only the first value is a number and other values are not numbers. Demo 2: We will see another demo to check whether a value is a number or not by utilizing the Number.isFinite() method. Code: Output: We can witness that the first & second values are a number and the third & values are not numbers. Utilizing the Number.isInteger() methodThe Number.isInteger() method can be utilized to check whether the variable value is numerical or not, but the Number.isInteger() method cannot check the floating point numbers. Demo 1: We will check if a value is a number or not utilizing the Number.isInteger() method. Code: Output: We can witness that the first and third values are not numbers but the second value is a number. Demo 2: We will see another demo of checking if a value is a number or not utilizing the Number.isInteger() method. Code: Output: We can witness that the first value is a number, the second value is not a number and the third value is a floating point number but not an integer which is why the Number.isInteger() method cannot detect the floating point number. Conclusion:We have understood the JavaScript Check Value is a number in this article. We have comprehended can check whether the variable value is a number utilizing various methods which are utilizing the typeof() operator, the isNaN() method, the Number.isFinite() method, and the Number.isInteger() method. Next TopicJavaScript Copy Object |