JavaScript

How to Find Variable Type in JavaScript

Variables are building blocks of any programming language as they allow a programmer/developer to write flexible code. In JavaScript, variables can hold numeric values(integers, floating points, etc.) or alphabetic values(characters, strings). Moreover, variables can represent the memory addresses. All in all, the variables book/reserve a spot in the memory to store various types of values. But do you know how to check the variable type in JavaScript? If not! Then nothing to worry about! By the end of this post, you will have a profound knowledge of finding variable types in javascript.

This article will cover the following topics regarding variable types in JavaScript:

So, let’s begin!

What is typeof in JavaScript

It is an operator that is used to find the type of an object or variable in JavaScript. The below snippet will show the basic syntax of typeof operator:

typeof variableName|objectName;

The above snippet shows that to check the variable/object type, we have to use the typeof operator followed by the variable or object name.

What is the need for typeof operator in JavaScript

As JavaScript is a dynamically typed programming language, so in JavaScript, there is no need to assign the variable type at the time of variable creation/declaration. It means in JavaScript; variables are not restricted to any data type. Hence their type can be changed at runtime. Therefore, sometimes we have to check the variable type in our program.

What does typeof operator return

If we use the typeof operator with any primitive data type then the typeof operator will return one of the below-listed data types:

  • String.
  • Boolean (for true or false).
  • Number (for int, float, etc.).
  • Undefined if a variable has no value.

The typeof operator will return one of the below-listed data type if we check the data type of the methods, objects, and arrays:

  • function (for methods).
  • object (for objects and arrays because in JavaScript, arrays are considered as objects).

How to find variable type in JavaScript

Now, let’s consider some examples for a profound understanding of the typeof operator in JavaScript.

Example1
How to find the type of a variable in JavaScript:

<script>
  var item = 10;
  var str = "Welcome to Linuxhint";
  var a;
  console.log(typeof item);
  console.log(typeof str);
  console.log(typeof a);
</script>

In the above code snippet, we utilized the typeof operator to find the type of different variables. Consequently, we will get the following output:

From the above snippet, we can observe that the typeof operator returns “number” for the first variable that holds a numeric value, “string” for the second variable, and undefined for the third variable because it doesn’t hold any value.

Example2

How to find the type of an object and array in JavaScript:

<script>
  var object = new String();
  var array = [50, 26, 43, 74, 5];
  console.log(typeof object);
  console.log(typeof array);
</script>

In the above code block, we utilized the typeof operator with an object, and array. As a result, we will get the following output:

From the output, it is clear that the typeof operator is working appropriately.

Example3

How to find the type of a function in JavaScript:

<script>
  console.log(typeof function () {});
</script>

Following will be the output for the above-given lines of code:

This is how the typeof operator works in JavaScript.

Conclusion

In JavaScript, the typeof operator is used to find the type of a variable, object, or method. The typeof operator returns one of the following types: number, boolean, object, string, undefined, function. To find the type of any variable, all you need to do is, write typeof > a space “ ” > and specify the variable or object whose type you want to check. This write-up provided a detailed guide for finding the variable type in java.

About the author

Anees Asghar

I am a self-motivated IT professional having more than one year of industry experience in technical writing. I am passionate about writing on the topics related to web development.