How to check if a variable is a number in javascript?

How to check if a variable is a number in javascript?

Overview

In this tutorial, we will explain how to check whether a variable or given value is number or not in javascript. In this tutorial, you will learn easy two ways to check a variable or given value is number or not.

Here, we will provide a test demo to checking a given value or variable is number or not. So try these examples use it.

javaScript check if variable is a number:

  • isNaN() JavaScript – isNaN Stands for “is Not a Number”, if a variable or given value is not a number, it returns true, otherwise it will return false.
  • typeof JavaScript – If a variable or given value is a number, it will return a string named “number”. In JavaScript, the typeof operator returns the data type of its operand in the form of a string.

1. isNaN JavaScript

Let’s take a new example of JavaScript’s “isNaN” method.

<!DOCTYPE html>
<html>
<title>Check if variable is a number or not in JavaScript Using isNaN()</title>
<head></head>
<body>
<h1>Check if variable is a number or not in JavaScript Using isNaN()</h1>

<input type="text" id="demo" value="" placeholder="Please enter here.....">

<button onclick="myFunction()">Click &amp; See Result</button>

<script type="text/javascript">
    function myFunction() {
      var num = document.getElementById("demo").value; 
      console.log(num);
       if(isNaN(num)){
        alert(num + " is not a number");
       }else{
         alert(num + " is a number");
       }
  }

</script>

</body>
</html>

Demo

Check if variable or given value is a number or not in JavaScript Using isNaN()
Check if variable or given value is a number or not in JavaScript Using isNaN()



2. typeof JavaScript

Let’s take a new example of JavaScript’s “typeof” operator.

<!DOCTYPE html>
<html>
<title>Check if variable or given value is a number or not in JavaScript Using javascript Typeof</title>
<head></head>
<body>
<h1>Check if variable or given value is a number or not in JavaScript Using javascript Typeof</h1>

<input type="text" id="demo1" value="" placeholder="Please enter here.....">

<button onclick="javascriptTypeof()">Click &amp; See Result</button>

<script type="text/javascript">
    function javascriptTypeof() {
      var num = document.getElementById("demo1").value; 

      if(typeof num == 'number'){
        alert(num + " is a number");
       }else{
        alert(num + " is not a number");
       }
  }

</script>

</body>
</html>

Demo

Check if variable or given value is a number or not in JavaScript Using javascript Typeof operator
Check if variable or given value is a number or not in JavaScript Using javascript Typeof Operator



Conclusion

In this tutorial, you have learned easy two ways to check a given value is number or not with example and live demos.

You may like

  1. Check if Number Is Integer JavaScript|Number.isInteger
  2. PHP empty() – Check Variable is Empty in PHP
  3. JavaScript parseFloat | Convert String into Float Number
  4. JavaScript parseInt | Convert a String to an Integer Number
  5. JavaScript .toFixed() Method with Examples

If you have any questions or thoughts to share, use the comment form below to reach us.

AuthorTuts Make

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Javascript, JQuery, Laravel, Livewire, Codeigniter, Vue JS, Angular JS, React Js, WordPress, and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published. Required fields are marked *