How to return the data type of variable in JavaScript ?
Last Updated :
15 Jan, 2024
To return the JavaScript data type of a variable we can use the JavaScript typeof operator. 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
let x = 4242;
console.log(x);
x = "GeeksforGeeks" ;
console.log(x)
x = {
k: 4245,
a: "geeks"
};
console.log(x)
|
Output
4242
GeeksforGeeks
{ k: 4245, a: 'geeks' }
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
let x = 12345;
console.log( typeof (x));
|
Example 2: In this example, we will print the type of a string in the console.
Javascript
let x = "GeeksforGeeks" ;
console.log( typeof (x));
|
Output:
string
Example 3: In this example, we will print the type of various variables in the console.
Javascript
let 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))
|
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
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" )
|
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
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" )
|
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
let x = "hello"
console.log(x)
let y = 10
console.log(y)
z = x * y
console.log(z)
console.log( typeof (z))
|
Output
hello
10
NaN
number
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...