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
typeofoperator is followed by thevaluewhose type you want to determine. - The
valuecan be a variable, a literal, or an expression. - The
typeofoperator returns a string that represents the type of thevalue. - The returned string can be one of the following values:
"undefined": If thevalueisundefined."boolean": If thevalueis a boolean."number": If thevalueis a number."string": If thevalueis a string."symbol": If thevalueis a symbol."function": If thevalueis a function."object": If thevalueis an object ornull."bigint": If thevalueis a BigInt."object": If thevalueis 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
typeofoperator to determine the type of different values such as strings, numbers, booleans, arrays, objects,undefined, andnull. - The
typeofoperator returns the type of the value as a string. - The
typeofoperator 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
typeofoperator is useful when you want to handle different types of values differently in your code. - The
typeofoperator can be used to determine the type of a variable or a value at runtime, which can be useful in dynamic programming scenarios. - The
typeofoperator 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
typeofoperator can be used to handle different types of values differently in your code. - The
typeofoperator can be used to determine the type of a variable or a value at runtime, which can be useful in dynamic programming scenarios. - The
typeofoperator 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
typeofoperator to determine the type of thegreetfunction. - The
typeofoperator returns"function"because thegreetvalue is a function. - The
typeofoperator 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
typeofoperator can be used to determine the type of a function at runtime, which can be useful in dynamic programming scenarios. - The
typeofoperator 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
typeofoperator 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
typeofoperator to determine the type of thesymbolvalue, which is a symbol. - The
typeofoperator returns"symbol"because thesymbolvalue is a symbol. - The
typeofoperator 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
typeofoperator can be used to determine the type of a symbol at runtime, which can be useful in dynamic programming scenarios. - The
typeofoperator 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
typeofoperator 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.