Last modified: May 26, 2026
JavaScript Define Variable Type
JavaScript is a dynamically typed language. This means you do not need to declare the type of a variable when you create it.
The type of a variable is determined automatically at runtime based on the value you assign to it. This makes JavaScript flexible but also requires you to understand how types work.
In this article, you will learn how to define, check, and work with variable types in JavaScript. We will cover common types, the typeof operator, type conversion, and best practices.
What Are JavaScript Variable Types?
JavaScript has seven primitive data types and one complex type (Object). The primitive types are: String, Number, Boolean, Undefined, Null, Symbol, and BigInt.
When you define a variable, you simply assign a value. JavaScript automatically sets the type based on that value.
// Defining variables of different types
let name = "Alice"; // String
let age = 30; // Number
let isStudent = false; // Boolean
let city; // Undefined (no value assigned)
let data = null; // Null (intentional empty value)
let id = Symbol("id"); // Symbol (unique identifier)
let bigNumber = 123n; // BigInt (for large integers)
Notice that you do not specify a type. JavaScript infers it from the value.
Checking Variable Types with typeof
The typeof operator is the most common way to check the type of a variable. It returns a string indicating the type.
let score = 95;
console.log(typeof score); // "number"
let greeting = "Hello";
console.log(typeof greeting); // "string"
let isLogged = true;
console.log(typeof isLogged); // "boolean"
let notDefined;
console.log(typeof notDefined); // "undefined"
let emptyValue = null;
console.log(typeof emptyValue); // "object" (this is a known JavaScript bug)
One important thing to remember: typeof null returns "object". This is a historical quirk in JavaScript. For null checking, use strict equality ===.
For more detailed examples, check out our JavaScript Variables Examples guide.
Dynamic Typing in Action
Because JavaScript is dynamically typed, you can change the type of a variable by reassigning it a different value.
let value = "Hello"; // value is a string
console.log(typeof value); // "string"
value = 42; // value is now a number
console.log(typeof value); // "number"
value = true; // value is now a boolean
console.log(typeof value); // "boolean"
This flexibility is powerful, but it can also lead to bugs if you are not careful. Always be aware of what type your variable holds.
Type Conversion: Changing Types Explicitly
Sometimes you need to convert a variable from one type to another. JavaScript provides built-in functions for this.
Convert to string using String() or .toString().
let num = 100;
let strNum = String(num); // "100"
console.log(typeof strNum); // "string"
let bool = true;
let strBool = bool.toString(); // "true"
console.log(typeof strBool); // "string"
Convert to number using Number(), parseInt(), or parseFloat().
let str = "42";
let numFromStr = Number(str); // 42
console.log(typeof numFromStr); // "number"
let floatStr = "3.14";
let floatNum = parseFloat(floatStr); // 3.14
console.log(typeof floatNum); // "number"
Convert to boolean using Boolean(). Values like 0, null, undefined, NaN, and empty string become false. All other values become true.
console.log(Boolean(0)); // false
console.log(Boolean("")); // false
console.log(Boolean(null)); // false
console.log(Boolean(1)); // true
console.log(Boolean("Hello")); // true
For a deeper dive into working with strings, see our JavaScript Variable as String Guide.
Special Types: Undefined vs Null
Undefined means a variable has been declared but not assigned a value. Null is an intentional assignment representing "no value".
They are both falsy, but they are not equal when using strict equality.
let a;
console.log(a); // undefined
let b = null;
console.log(b); // null
console.log(a == b); // true (loose equality)
console.log(a === b); // false (strict equality)
Always use strict equality === to check for null or undefined to avoid confusion.
Objects: The Complex Type
Objects are collections of key-value pairs. Arrays, functions, and dates are all objects in JavaScript.
let person = { name: "Bob", age: 25 };
console.log(typeof person); // "object"
let colors = ["red", "green", "blue"];
console.log(typeof colors); // "object"
let greet = function() { return "Hi"; };
console.log(typeof greet); // "function"
Note that typeof returns "function" for functions, even though functions are technically objects.
Best Practices for Defining Variable Types
Even though JavaScript does not require explicit type declarations, following some best practices helps avoid errors.
- Use const for values that should not change. This also fixes the type.
- Use let for variables that will be reassigned.
- Avoid using var due to its function-scoping issues.
- Always initialize variables when you declare them to avoid undefined.
- Use
typeofchecks before performing type-specific operations.
// Good practice
const PI = 3.14159; // constant number
let username = "John"; // string, may be reassigned
// Type check before operation
function doubleValue(val) {
if (typeof val === "number") {
return val * 2;
} else {
return "Value is not a number";
}
}
console.log(doubleValue(5)); // 10
console.log(doubleValue("5")); // "Value is not a number"
For a complete overview of variable types, read our JavaScript Variable Types Guide.
Common Pitfalls with Types
One common mistake is assuming a variable's type stays the same. Always verify with typeof.
Another pitfall is using loose equality == which performs type coercion. This can lead to unexpected results.
console.log(0 == false); // true (coercion)
console.log(0 === false); // false (no coercion)
console.log("" == 0); // true
console.log("" === 0); // false
Using === avoids these issues and makes your code more predictable.
Conclusion
Understanding how to define and check variable types is essential for writing reliable JavaScript code.
Remember that JavaScript is dynamically typed, so types are determined by values. Use the typeof operator to inspect types, and use strict equality for comparisons.
Always be mindful of type conversion and the quirks like typeof null returning "object". With practice, you will handle types confidently.
Start applying these concepts today to write cleaner and more predictable JavaScript programs.