JavaScript Check if Variable is a Number Example
1. Introduction
JavaScript is referred to as a dynamically typed language. What that means is that the language has data types but variables can hold values of different data types at different points in time. We can declare a variable and assign a string and subsequently assign a number to it as follows:
let name = "Siddharth"; name = 39;
The behavior displayed in the above snippet makes JavaScript a dynamically typed language. Lisp, Groovy are some of the other dynamically typed languages out there. This brings us to the point of this article. How to check if a variable at a point in time contains a number or numeric value.
In this article we will learn about type checking in JavaScript. Specifically, we will learn how to check if a variable is a Number. Let’s get started without further delay.
2. JavaScript Check if Variable is a Number
We can perform this check using the typeof
operator in JavaScript. The typeof operator returns a string which indicates the type of the operand provided. JavaScript has the following data types:
- Number
- Boolean
- String
- Function
- Object
Apart from the above there is the value NaN
.
So, let’s build a utility to type check a variable and return it’s type. Our NodeJS application structure would look as follows:
The code for typeChecker module looks like below:
typeChecker.js
function typeCheck(variable) { return typeof variable; } function isNumber(variable) { return typeCheck(variable) === "number" && !isNotaNumber(variable); } function isNotaNumber(variable) { return isNaN(variable); } module.exports.typeCheck = typeCheck; module.exports.isNumber = isNumber; module.exports.isNotaNumber = isNotaNumber;
In the above code we have an additional check for isNaN because the value NaN has a type of Number. Now let us consume the small module we have created to see it in action
index.js
const typeChecker = require('./typeChecker'); let num = -10; num1 = '222.33'; num2 = 9090.99; num3 = NaN; num4 = 0; num5 = undefined; num6 = []; num7 = function () { } console.log(typeChecker.typeCheck(num)); console.log(typeChecker.isNumber(num)); console.log(typeChecker.typeCheck(num1)); console.log(typeChecker.isNumber(num1)); console.log(typeChecker.typeCheck(num2)); console.log(typeChecker.isNumber(num2)); console.log(typeChecker.typeCheck(num3)); console.log(typeChecker.isNumber(num3)); console.log(typeChecker.typeCheck(num4)); console.log(typeChecker.isNumber(num4)); console.log(typeChecker.typeCheck(num5)); console.log(typeChecker.isNumber(num5)); console.log(typeChecker.typeCheck(num6)); console.log(typeChecker.isNumber(num6)); console.log(typeChecker.typeCheck(num7)); console.log(typeChecker.isNumber(num7));
We run the above application using the command below:
>node index.js
We should see the below output:
As we can see from the above output, when we pass in the variables num through to num7 we get the appropriate output.
3. Download the Source Code
This was an example of JavaScript Check if Variable is a Number Example:
You can download the full source code of this example here: JavaScript Check if Variable is a Number Example