• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • VBA
  • About
You are here: Home / JavaScript / Using JavaScript to Check if Variable is a Number

Using JavaScript to Check if Variable is a Number

June 13, 2022 Leave a Comment

One of the easiest ways we can use JavaScript to check if a variable is a number is by using the JavaScript typeof Operator in an if conditional statement.

if ( typeof someVariable == "number" ){
  //The variable IS a number
}

In the code above, someVariable is the variable that we want to check to see if it is a number. typeof someVariable will return “number” if the variable is a number, and therefore the conditional statement will be true.

We can put this code in a function to make checking if a variable is a number very simple.

Here is the function:

function isNumber(variable){
  if ( typeof variable == "number" ){
    return true;
  } else {
    return false;  
  }
};

Our function simply returns true if the variable you enter is a number, and false if not.

Now let’s show some examples of this function is use:

function isNumber(variable){
  if ( typeof variable == "number" ){
    return true;
  } else {
    return false;  
  }
};

var variable1 = 1234;
var variable2 = -853;
var variable3 = "1234";
var variable4 = 156.430393;
var variable5 = 0045865;
var variable6 = .00098;

console.log(isNumber(variable1));
console.log(isNumber(variable2));
console.log(isNumber(variable3));
console.log(isNumber(variable4));
console.log(isNumber(variable5));
console.log(isNumber(variable6));

#Output:
true
true
false
true
true
true

Hopefully this article has been useful for you to learn how to use JavaScript to check if a variable is a number.

Other Articles You'll Also Like:

  • 1.  Using JavaScript to Check if Variable Exists
  • 2.  Using JavaScript to Get Image Dimensions
  • 3.  Using JavaScript to Capitalize the First Letter of a String
  • 4.  Using JavaScript to Count Even Numbers in an Array
  • 5.  Using JavaScript to Multiply All Elements in an Array
  • 6.  Finding the Length of a String in JavaScript
  • 7.  How to Split a Number into Digits in JavaScript
  • 8.  JavaScript isInteger – How to Check if a Number is an Integer
  • 9.  Using JavaScript to Show a Div
  • 10.  JavaScript Opacity – How to Change the Opacity of an Element Using JavaScript

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

About The Programming Expert

the programming expert main image

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2022 · The Programming Expert · About · Privacy Policy