How to Get Type of a Variable in JavaScript
JavaScript is a well-known dynamically typed language that can change the date type during runtime. However, it is important to find the type of a variable because users can not specify the type of a variable at declaration time. The typeof operator is employed to get the variable type in JavaScript. This article aims to provide a brief explanation of how to get the type of a variable in JavaScript with the help of suitable examples.
How to Get Type of a Variable in JavaScript?
JavaScript provides a typeof operator that has enumerable features to evaluate the type of variable. It returns the data type by passing operands to it. The syntax of the typeof operator is simple and is described below:
Syntax:
In this syntax, the operand represents the expression or variable to find the data type.
Note: JavaScript offers the following data types for variables to contain:
Data Types | Description |
Number | Integer or Floating number. |
Boolean | True or False values. |
Undefined | Data type whose value is not initialized. |
String | Specifies the textual data. |
Object | Key-value pairs of collection of data. |
Function | Assign function as a variable. |
Let us practice the use of the typeof operator to check the datatype of the various variables.
Example 1: Getting the Boolean, Number and String Types in JavaScript
An example illustrates the working of typeof operators with different data types (Boolean, Number, and String) in JavaScript. The code is utilized for getting the types of different variables.
Code
var1 = false;
console.log(typeof(var1));
let var2 =45418761;
console.log(typeof(var2));
var3 = "Welcome to JavaScript World";
console.log(typeof(var3));
The code is described in listed order:
- Firstly, “var1” stores the Boolean value “false” and the “typeof” operator is used to evaluate the type of “var1”.
- The“var2” and “var3” store the numbers “45418761” and string “Welcome to JavaScript World”.
- The data types of “var2” and “var3” variables are retrieved using the “typeof” operator.
Output
The output returns the data type of “var1”, “va2,” and “var3” variables as boolean, number, and string respectively.
Example 2: Getting the Function and Object Types in JavaScript
Another example is considered to compute the data types of two variables via typeof operators. The code is provided here.
Code
let var1 = function() { return 2*2 } ;
console.log(typeof(var1));
var2 = [23,242,63];
console.log(typeof(var2));
In the above code:
- Firstly, a function is called that returns the “2*2” and stores it in the “var1” variable.
- The typeof operator computes the type of “var1” variable and displays the output.
- Similarly, “var2” stores an array “[23, 242, 63]” and prints the type of “var2” by passing through the “typeof” operator.
Output
The output shows that the “function” and “object” types of “var1” and “var2” variables are displayed in the console window.
Conclusion
In JavaScript, the typeof operator can be utilized to get the data types of variables. Users can pass variables as arguments and return types such as boolean, number, string, function, object, etc. This article utilizes the typeof operator to extract the data type of the variable along with different examples. This operator is useful for developers to check the data types during the runtime. Here, you have learned to get the variable types in JavaScript.
Source: linuxhint.com