How to return the data type of variable in JavaScript ? - GeeksforGeeks

09 Apr, 2021
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
Javascript
<script>      // x is a number    var x = 4242;    console.log(x);      // x is a string    x = "GeeksforGeeks";    console.log(x)      // x is an object    x = {        k: 4245,        a: "geeks"    };     console.log(x)</script>
Output:
4242
GeeksforGeeks
{k: 4245, a: "geeks"}
As seen in the above 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.
typeof: 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:
Javascript
<script>      var x = 12345;    console.log(typeof(x));      </script>
Output:
number
Example 2:
Javascript
<script>      var x = "GeeksforGeeks";    console.log(typeof(x));  </script>
Output:
string
Example 3:
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:
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>
Invoke the above function with number  and string as an argument.
Output:
x is a number
x is a string
Another use of typeof operator is to check if a variable is declared before it’s usage.
Example:
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>
Invoking the above function without passing an argument and by passing a string as an argument.
Output:
x is undefined. Please declare it
We can process x!
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.
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
javascript-operators
JavaScript-Questions
Picked
JavaScript
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Remove elements from a JavaScript Array
Difference between var, let and const keywords in JavaScript
Difference Between PUT and PATCH Request
JavaScript | Promises
How to get character array from string in JavaScript?
Remove elements from a JavaScript Array
Installation of Node.js on Linux
How to fetch data from an API in ReactJS ?
Top 10 Projects For Beginners To Practice HTML and CSS Skills
Difference between var, let and const keywords in JavaScript