Last modified: May 13, 2026 By Alexander Williams

JavaScript Get Type of Variable

Understanding the type of a variable is a fundamental skill in JavaScript. It helps you write safer and more predictable code. This guide will show you how to get the type of a variable using three main methods: typeof, instanceof, and Object.prototype.toString.

Why Check Variable Types?

JavaScript is a dynamically typed language. A variable can hold any type of data at any time. This flexibility can lead to unexpected bugs. Checking the type of a variable helps you avoid errors and handle data correctly.

For example, you might receive data from an API. You need to know if it's a string, number, or object before processing it. Type checking gives you that control.

The typeof Operator

The typeof operator is the most common way to check a variable's type. It returns a string indicating the type of the operand.

 
// Using typeof operator
let name = "Alice";
console.log(typeof name); // "string"

let age = 30;
console.log(typeof age); // "number"

let isStudent = true;
console.log(typeof isStudent); // "boolean"

let car = null;
console.log(typeof car); // "object" (this is a known bug)

let job;
console.log(typeof job); // "undefined"

let person = { name: "Bob" };
console.log(typeof person); // "object"

let numbers = [1, 2, 3];
console.log(typeof numbers); // "object" (arrays are objects)

Important: typeof has quirks. It returns "object" for null and arrays. This is a well-known JavaScript behavior. Use other methods for these cases.

Handling null and Arrays

Since typeof returns "object" for both null and arrays, you need extra checks. For null, you can compare directly. For arrays, use Array.isArray().

 
// Checking for null
let empty = null;
if (empty === null) {
    console.log("Variable is null");
}

// Checking for arrays
let fruits = ["apple", "banana"];
if (Array.isArray(fruits)) {
    console.log("Variable is an array");
}

For more details on declaring variables, read our JavaScript Variable Declaration Guide.

The instanceof Operator

The instanceof operator checks if an object is an instance of a specific constructor. It works well for custom objects and built-in types like Date and RegExp.

 
// Using instanceof operator
let today = new Date();
console.log(today instanceof Date); // true

let pattern = /hello/;
console.log(pattern instanceof RegExp); // true

let arr = [1, 2, 3];
console.log(arr instanceof Array); // true
console.log(arr instanceof Object); // true (arrays are objects)

instanceof checks the prototype chain. This means an array is also an instance of Object. Use it carefully when you need exact types.

Using Object.prototype.toString

For the most accurate type checking, use Object.prototype.toString.call(). This method returns a string like "[object Type]". It works for all built-in types.

 
// Accurate type checking with toString
function getType(value) {
    return Object.prototype.toString.call(value).slice(8, -1);
}

console.log(getType("hello"));    // "String"
console.log(getType(42));         // "Number"
console.log(getType(true));       // "Boolean"
console.log(getType(null));       // "Null"
console.log(getType(undefined));  // "Undefined"
console.log(getType([1, 2]));     // "Array"
console.log(getType({}));         // "Object"
console.log(getType(new Date())); // "Date"

This method is reliable and avoids the quirks of typeof. It's perfect for type validation in production code.

Practical Examples

Let's see how to combine these methods in real scenarios. Imagine you are building a function that processes user input.

 
// Practical type checking function
function processInput(input) {
    // Check if input is a string
    if (typeof input === "string") {
        console.log("Processing string:", input.toUpperCase());
    }
    // Check if input is a number
    else if (typeof input === "number") {
        console.log("Processing number:", input * 2);
    }
    // Check if input is an array
    else if (Array.isArray(input)) {
        console.log("Processing array:", input.join(", "));
    }
    // Check if input is an object
    else if (typeof input === "object" && input !== null) {
        console.log("Processing object:", JSON.stringify(input));
    }
    else {
        console.log("Unknown type");
    }
}

processInput("hello");  // "Processing string: HELLO"
processInput(10);       // "Processing number: 20"
processInput([1, 2, 3]);// "Processing array: 1, 2, 3"
processInput({ key: "value" }); // "Processing object: {"key":"value"}"

Notice the order of checks. We handle strings and numbers first. Then arrays, then objects. This prevents false positives from typeof returning "object" for arrays.

Common Pitfalls

Here are some mistakes beginners make when checking types:

  • Using typeof for arrays: Always use Array.isArray() instead.
  • Forgetting null check: typeof null returns "object", so always check with === null.
  • Confusing primitive and object wrappers: typeof "hello" is "string", but typeof new String("hello") is "object".
 
// Pitfall example
let strPrimitive = "hello";
let strObject = new String("hello");

console.log(typeof strPrimitive); // "string"
console.log(typeof strObject);    // "object"
console.log(strPrimitive === strObject); // false

Always prefer primitive values unless you have a specific reason to use wrapper objects.

Best Practices

Follow these tips for clean and reliable type checking:

  • Use typeof for primitive types: string, number, boolean, undefined, symbol, bigint.
  • Use Array.isArray() for arrays.
  • Use === null for null values.
  • Use Object.prototype.toString for accurate type detection of built-in objects.
  • Use instanceof for custom class instances.

For a complete overview of variables, check our JavaScript Variables Guide.

Conclusion

Knowing how to get the type of a variable in JavaScript is essential for writing robust code. The typeof operator is fast and works for primitives. Use Array.isArray() and === null for special cases. For full accuracy, Object.prototype.toString is your best friend. Practice these methods to avoid bugs and build better applications.