Programming Language
JavaScript
Variables
Return Data Type

How to Check What Type of Data a Variable Has in JavaScript

What is This About?

In JavaScript, you can put different types of data in variables. Sometimes you need to know what type of data is in a variable. JavaScript has a special word called typeof that helps you do this.

How to Find the Data Type of a Variable in JavaScript?

In JavaScript, we can determine the type of a variable using the typeof operator.

Unlike many programming languages, JavaScript does not require you to define a variable’s type when declaring it. Instead, the type is automatically inferred from the value it holds. Since JavaScript is a dynamically typed language, a variable’s type can change during program execution.

Why Do We Need This?

In JavaScript, you don't have to say what type of data a variable will hold. The computer figures it out by itself. But sometimes the same variable can hold different types of data at different times.

Example: Same Variable, Different Types

// x has a number
let x = 4242;
console.log(x);
 
// Now x has text
x = "JavaScript";
console.log(x);
 
// Now x has an object
x = {
    k: 4245,
    a: "js"
};
console.log(x);

What you see:

4242
JavaScript
{ k: 4245, a: 'js' }

This makes it hard to keep track of what type of data 'x' has throughout your program.

The typeof Operator

The typeof keyword helps you find out what type of data a variable has. Since JavaScript figures out types by itself, typeof is very useful to check what type you're working with.

Example 1: Check if Something is a Number

let x = 12345;
console.log(typeof(x));

What you see:

number

Example 2: Check if Something is Text

let x = "JavaScript";
console.log(typeof(x));

What you see:

string

Example 3: Check Different Parts of an Object

let x = { k: 12, m: "geeky stuff" };
console.log(typeof(x));      // the whole object
console.log(typeof(x.k));    // the number inside
console.log(typeof(x.m));    // the text inside
console.log(typeof(x.s));    // something that doesn't exist

What you see:

object
number
string
undefined

Using typeof in Functions

You can use typeof inside functions to do different things based on what type of data you get.

Example: Do Different Things for Different Types

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 js");

What you see:

x is a number
x is a string

Example: Check if Something Exists Before Using It

function checkX(x) {
    if (typeof(x) === "undefined") {
        console.log("x is undefined. Please declare it");
    } else {
        console.log("We can process x!");
    }
}
 
checkX();           // no data given
checkX("hello");    // data given

What you see:

x is undefined. Please declare it
We can process x!

Something Weird About Numbers

There's something strange in JavaScript. When you try to do math with text and numbers, you get something called NaN (which means "Not a Number"). But typeof still says it's a number!

let x = "hello";
console.log(x);
 
let y = 10;
console.log(y);
 
let z = x * y;
console.log(z);
console.log(typeof(z));

What you see:

hello
10
NaN
number

Even though NaN means "Not a Number", typeof still returns "number". This is confusing but that's how JavaScript works!

All the Types You Can Get

When you use typeof, you can get these answers:

  • "number" - for numbers (like 5, 3.14, or even NaN)
  • "string" - for text (like "hello")
  • "boolean" - for true/false
  • "undefined" - for things that don't exist
  • "object" - for objects, arrays, and null
  • "function" - for functions

More Examples

Here are more examples to help you understand:

console.log(typeof 42);           // "number"
console.log(typeof "hello");      // "string"
console.log(typeof true);         // "boolean"
console.log(typeof undefined);    // "undefined"
console.log(typeof {});           // "object"
console.log(typeof []);           // "object" (arrays are objects too!)
console.log(typeof null);         // "object" (this is weird but true)
console.log(typeof function(){}); // "function"

When to Use typeof

  1. Before doing math: Check if something is a number before adding or multiplying
  2. Before using text methods: Check if something is a string before using .length or .toUpperCase()
  3. Before accessing object properties: Check if something exists before trying to use it
  4. In functions: Check what type of data was passed to your function

Simple Tips

  1. Use typeof when you need to know what type of data you have
  2. Remember that typeof null gives you "object" (this is weird but true)
  3. Remember that typeof NaN gives you "number" even though NaN means "Not a Number"
  4. Arrays show up as "object" when you use typeof
  5. Always put parentheses around the variable: typeof(x) or write it like typeof x

Real-World Example

function processData(data) {
    if (typeof(data) === "number") {
        console.log("I'll do math with this number: " + (data * 2));
    } else if (typeof(data) === "string") {
        console.log("I'll make this text uppercase: " + data.toUpperCase());
    } else if (typeof(data) === "undefined") {
        console.log("You didn't give me any data!");
    } else {
        console.log("I don't know what to do with this type: " + typeof(data));
    }
}
 
processData(5);           // number
processData("hello");     // string
processData();            // undefined
processData(true);        // boolean

What you see:

I'll do math with this number: 10
I'll make this text uppercase: HELLO
You didn't give me any data!
I don't know what to do with this type: boolean

That's it! Now you know how to check what type of data your variables have in JavaScript using the typeof operator.