Skip to content
Related Articles

Related Articles

How to return the data type of variable in JavaScript ?
  • Last Updated : 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
My Personal Notes arrow_drop_up
Recommended Articles
Page :