Definition and Usage
The typeof
operator returns a string that represents the data type of the operand.
It is used to check the type of a variable or value.
Basic Example
let a;
console.log(typeof a); // Output: "undefined"
console.log(typeof true); // Output: "boolean"
console.log(typeof 42); // Output: "number"
console.log(typeof "Hello"); // Output: "string"
This example demonstrates how the typeof
operator returns a string that indicates the type of a given value. The returned value is always one of JavaScript's predefined type names, such as "undefined"
, "boolean"
, "number"
, or "string"
.
Syntax
typeof operand
operand
is the value or expression whose data type you want to determine.
More precisely, it represents an object or a primitive value whose data type you want to check. The operand
appears to the right of the typeof
operator. When evaluated, typeof
returns a string representing the data type of the evaluated operand
.
The operand
can be a variable, constant, literal value, function call, or any other expression.
let x = 42;
let y = "Hello";
let z = {key: "value"};
console.log(typeof x); // Output: "number"
console.log(typeof y); // Output: "string"
console.log(typeof z); // Output: "object"
In the example above, x
, y
, and z
are variables holding different data types. The typeof
operator takes each variable as its operand
and returns the corresponding data type as a string.
In summary, the operand
refers to the value or expression that the typeof
operator evaluates to determine its data type.
Data Types and Their typeof
Results
Data Type | Result |
---|---|
number | "number" |
string | "string" |
boolean | "boolean" |
undefined | "undefined" |
object | "object" |
function | "function" |
symbol | "symbol" |
bigint | "bigint" |
typeof
Results
/* Numbers */
typeof 24 === "number"
typeof 3.14 === "number"
typeof NaN === "number"
typeof parseInt("10px") === "number"
typeof Number("2") === "number"
typeof Number("text") === "number"
/* Strings */
typeof "coding" === "string"
typeof "" === "string"
typeof `template literal` === "string"
typeof "24" === "string"
typeof String(24) === "string"
/* Booleans */
typeof true === "boolean"
typeof false === "boolean"
typeof Boolean(24) === "boolean"
typeof !!24 === "boolean" // Double negation (!!) converts value to Boolean
/* Undefined */
var x;
typeof x === "undefined";
typeof undefined === "undefined";
typeof y === "undefined"; // Returns "undefined" even for undeclared variables
/* Objects */
typeof {param: 1} === "object";
typeof {} === "object";
typeof [1, 2, 3] === "object"; // Arrays also return "object"
typeof [] === "object"; // Empty arrays also return "object"
typeof /regex/ === "object"; // Regular expressions return "object"
typeof null === "object" // null also returns "object"
/* Functions */
function $() {}
typeof $ === "function";
typeof function () {} === "function";
typeof class ClassName {} === "function";
/* Symbols */
typeof Symbol() === "symbol";
typeof Symbol("foo") === "symbol";
/* BigInts */
typeof 1n === "bigint";
typeof BigInt("1") === "bigint";
Important Considerations When Using the typeof
Operator
Checking for null
The typeof
operator returns "object"
for null
. This is a known peculiarity and differs from the actual data type null
.
console.log(typeof null); // Output: "object"
Regular Expressions
The typeof
operator returns "object"
for regular expressions because they are internally treated as objects.
let regex = /[a-zA-Z]/;
console.log(typeof regex); // Output: "object"
Checking for Arrays
In JavaScript, it is common to check if a variable or value is an array.
Since arrays are a type of object, the typeof
operator returns "object"
for arrays, making it insufficient to reliably determine if a value is an array.
const arr = [1, 2, 3];
console.log(typeof arr); // Output: "object"
The most reliable way to check if a value is an array is to use the Array.isArray()
function, which returns true
if the argument is an array, and false
otherwise.
const arr = [1, 2, 3];
console.log(Array.isArray(arr)); // true
When a Variable Is Not Declared – Returns "undefined"
Using typeof
on an undeclared variable does not throw an error; instead, it returns "undefined"
.
// Variable x has never been declared
console.log(typeof x); // "undefined"
It is important to note that the undefined
data type means that a declared variable has not been assigned a value, not that the variable is undeclared. However, typeof
returns "undefined"
for both cases.
This behavior requires caution, as undeclared variables and declared-but-uninitialized variables are treated the same way, which can lead to incorrect assumptions.
A safer way to check if a variable is declared is by using a try...catch
block.
try...catch
block
/* Variable x has been declared but not assigned a value */
let x;
if (x === undefined) {
console.log("Evaluates to true.");
} else {
console.log("Evaluates to false.");
}
// Output: "Evaluates to true."
/* Variable y is undeclared */
try {
y; // Attempt to access variable y
console.log("Variable is declared.");
} catch (error) {
console.log("Variable is not declared.");
}
// Output: "Variable is not declared."
Practical Examples
In real-world development, it is often necessary to check whether external JavaScript libraries like jQuery have been properly loaded. Accessing an undefined global variable directly typically results in a ReferenceError
. However, using the typeof
operator allows you to safely check the variable without causing an error.
The following example demonstrates how to check if the jQuery library code has been loaded on the page:
if (typeof jQuery === "function") {
console.log("jQuery is loaded and available.");
} else {
console.log("jQuery is not loaded.");
}
Explanation
typeof jQuery === "function"
checks if the global variablejQuery
exists and whether its type isfunction
.- Even if
jQuery
is not defined,typeof
returns the string"undefined"
, so the conditional statement executes without error. - If the jQuery library is correctly loaded,
typeof jQuery
returns"function"
because jQuery is implemented as a function.
This technique is especially useful when loading external libraries via CDN or when you want to optionally check dependencies in environments where the jQuery library is not mandatory.
Specifications
Specification | |
---|---|
typeof
|
ECMAScript Language Specification #sec-typeof-operator |
Browser compatibility
Operator |
Desktop Chrome
|
DesktopDesktop Edge
|
Desktop Firefox
|
Safari
|
---|---|---|---|---|
typeof
|
1 | 12 | 1 | 1 |