To return the JavaScript data type of a variable we can use the JavaScript typeof operator. Because In JavaScript, unlike many other programming languages, we do not specify the type of a variable while declaring it, rather the variable’s type is automatically inferred based on the value it holds. In other words, JavaScript is a “dynamically typed” programming language. In such languages, the type of a variable can change throughout the program.
Example: In this example, x was initialized to a number, then we initialized it to a string, and then an object. This makes it difficult to keep track of the type of variable ‘x’ throughout the program.
Javascript
<script> // x is a number var x = 4242; console.log(x); // x is a string x = "neveropen"; console.log(x) // x is an object x = { k: 4245, a: "neveropen" }; console.log(x)</script> |
Output:
4242
neveropen
{k: 4245, a: "neveropen"}
JavaScript typeof Operator: The typeof keyword helps to determine the type of a variable in Javascript. Since Javascript is a dynamically typed programming language, typeof can be used to find the variable type.
It can be used within a function to check the data type of a variable or to check if a variable is declared. Let’s consider the following examples to understand this better.
Example 1: In this example, we will print the type of x in the console.
Javascript
<script> var x = 12345; console.log(typeof(x));</script> |
Output:
number
Example 2: In this example, we will print the type of a string in the console.
Javascript
<script> var x = "neveropen"; console.log(typeof(x));</script> |
Output:
string
Example 3: In this example, we will print the type of various variables in the console.
Javascript
<script> var x = { k : 12, m : "geeky stuff"} console.log(typeof(x)) console.log(typeof(x.k)) console.log(typeof(x.m)) console.log(typeof(x.s))</script> |
Output:
object number string undefined
A common use of typeof operator is to determine the variable type and perform actions accordingly within a function.
Example: Invoke the above function with a number and string as an argument. Another use of the typeof operator is to check if a variable is declared before its usage.
Javascript
<script> function doX(x) { if (typeof(x) === "number") { console.log("x is a number") } if (typeof(x) === "string") { console.log("x is a string") } if (typeof(x) === "undefined") { console.log("x is undefined") } } doX(12) doX("hello gfg")</script> |
Output:
x is a number x is a string
Example: Invoking the above function without passing an argument and by passing a string as an argument.
Javascript
<script> function checkX(x) { if (typeof(x) === "undefined") { console.log( "x is undefined. Please declare it"); } else { console.log("We can process x!") } } checkX() checkX("hello")</script> |
Output:
x is undefined. Please declare it We can process x!
Example: One small caveat with typeof is that typeof(NaN) returns a number. When we multiply a string with a number we get NaN, as seen in the below example.
Javascript
<script> var x = "hello" console.log(x) var y = 10 console.log(y) z = x * y console.log(z) console.log(typeof(z))</script> |
Output:
hello 10 NaN number
