How to check if a variable is a number in JavaScript

How to check if a variable is a number in JavaScript

In this post we will give you information about How to check if a variable is a number in JavaScript. Hear we will give you detail about How to check if a variable is a number in JavaScriptAnd how to use it also give you demo for it if it is necessary.

we are going to learn about how to check if a variable (including a string) is a number in JavaScript.

JavaScript has a built-in isNan() function where it returns false if a given variable is a number else it returns true if a given variable is not a number.

Example:

const num = 3;console.log(isNaN(num)); // falseconst strNum = '3';console.log(isNaN(strNum)); // falseconst str = 'react';console.log(isNaN(str)); // true

In the above code, we are getting false if a variable has a value string number ‘3’, But we need true to do this we need to create our own function called isNumeric.

function isNumeric(num){    return !isNaN(num); // added negation operator}console.log(isNumeric('3')); // trueconsole.log(isNumeric(3)); // trueconsole.log(isNumeric('fabric')) // false

Hope this code and post will helped you for implement How to check if a variable is a number in JavaScript. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US