typeof Operator in JavaScript
In JavaScript, the typeof
operator is used to determine the type of a value. It is often used when you want to check the type of a variable or a value before performing an operation on it.
Syntax
The syntax of the typeof
operator is as follows:
typeof value
- The
typeof
operator is followed by thevalue
whose type you want to determine. - The
value
can be a variable, a literal, or an expression. - The
typeof
operator returns a string that represents the type of thevalue
. - The returned string can be one of the following values:
"undefined"
: If thevalue
isundefined
."boolean"
: If thevalue
is a boolean."number"
: If thevalue
is a number."string"
: If thevalue
is a string."symbol"
: If thevalue
is a symbol."function"
: If thevalue
is a function."object"
: If thevalue
is an object ornull
."bigint"
: If thevalue
is a BigInt."object"
: If thevalue
is an object ornull
.
Example
The following example demonstrates how to use the typeof
operator to determine the type of a value:
const name = "John";
const age = 30;
const isMarried = false;
const fruits = ["apple", "banana", "cherry"];
const person = {
name: "John",
age: 30,
};
console.log(typeof name); // string
console.log(typeof age); // number
console.log(typeof isMarried); // boolean
console.log(typeof fruits); // object
console.log(typeof person); // object
console.log(typeof undefinedVariable); // undefined
console.log(typeof nullVariable); // object
- In this example, we have used the
typeof
operator to determine the type of different values such as strings, numbers, booleans, arrays, objects,undefined
, andnull
. - The
typeof
operator returns the type of the value as a string. - The
typeof
operator is often used to check the type of a value before performing an operation on it, such as type conversion or type-specific operations. - The
typeof
operator is useful when you want to handle different types of values differently in your code. - The
typeof
operator can be used to determine the type of a variable or a value at runtime, which can be useful in dynamic programming scenarios. - The
typeof
operator can be used to determine the type of a value before performing an operation on it, such as type conversion or type-specific operations. - The
typeof
operator can be used to handle different types of values differently in your code. - The
typeof
operator can be used to determine the type of a variable or a value at runtime, which can be useful in dynamic programming scenarios. - The
typeof
operator can be used to determine the type of a value before performing an operation on it, such as type conversion or type-specific operations.
Using the typeof
Operator with Functions
The typeof
operator can be used to determine the type of a function. It returns "function"
if the value is a function.
function greet() {
console.log("Hello, World!");
}
console.log(typeof greet); // function
- In this example, we have used the
typeof
operator to determine the type of thegreet
function. - The
typeof
operator returns"function"
because thegreet
value is a function. - The
typeof
operator can be used to determine the type of a function, which can be useful when you want to handle functions differently in your code. - The
typeof
operator can be used to determine the type of a function at runtime, which can be useful in dynamic programming scenarios. - The
typeof
operator can be used to determine the type of a function before performing an operation on it, such as calling the function or passing it as an argument to another function. - The
typeof
operator can be used to handle functions differently in your code based on their type.
Using the typeof
Operator with null
The typeof
operator returns "object"
if the value is null
. This is a historical bug in JavaScript, and it is not expected to change.
const person = null;
console.log(typeof person); // object
In this example, we have used the typeof
operator to determine the type of the person
value, which is null
. The typeof
operator returns "object"
because the person
value is null
.
The typeof
operator returns "object"
if the value is null
. This is a historical bug in JavaScript, and it is not expected to change.
The typeof
operator can be used to determine the type of a value at runtime, which can be useful in dynamic programming scenarios.
The typeof
operator can be used to determine the type of a value before performing an operation on it, such as type conversion or type-specific operations.
Using the typeof
Operator with Symbols
The typeof
operator returns "symbol"
if the value is a symbol.
const symbol = Symbol("mySymbol");
console.log(typeof symbol); // symbol
- In this example, we have used the
typeof
operator to determine the type of thesymbol
value, which is a symbol. - The
typeof
operator returns"symbol"
because thesymbol
value is a symbol. - The
typeof
operator can be used to determine the type of a symbol, which can be useful when you want to handle symbols differently in your code. - The
typeof
operator can be used to determine the type of a symbol at runtime, which can be useful in dynamic programming scenarios. - The
typeof
operator can be used to determine the type of a symbol before performing an operation on it, such as type conversion or type-specific operations. - The
typeof
operator can be used to handle symbols differently in your code based on their type.
The typeof
operator returns "object"
if the value is null
. This is a historical bug in JavaScript, and it is not expected to change.
Conclusion
In this tutorial, you learned about the typeof
operator in JavaScript and how to use it to determine the type of a value. You also learned about the different types of values that the typeof
operator can return, such as "undefined"
, "boolean"
, "number"
, "string"
, "symbol"
, "function"
, "object"
, and "bigint"
. You also learned about the historical bug in JavaScript where the typeof
operator returns "object"
for null
values. The typeof
operator is useful when you want to handle different types of values differently in your code, and it can be used to determine the type of a variable or a value at runtime, which can be useful in dynamic programming scenarios.