How to check the type of a variable or object in JavaScript ?
Last Updated :
31 Jan, 2022
In this article, How to Check the Type of a Variable or Object in JavaScript? In JavaScript, the typeof operator is used to determine the typeof an object or variable. JavaScript, on the other hand, is a dynamically typed (or weakly typed) language. This indicates that a variable can have any type of value. The type of the value assigned to a variable determines the type of the variable.
The typeof operator in JavaScript allows you to determine the type of value or type of value that a variable contains . There is just one operand for the typeof operator (a unary operator), which takes one variable as input. It determines the operand's type and a string is returned as a result.
Let's understand the typeof operator using some examples:
Example 1: If a string variable is checked by typeof, the result will be "string".
JavaScript
<script>
var apple = "apple";
console.log(typeof apple);
</script>
Output:
"string"
Example 2: When we check the type of a number variable it results in the string "number".
JavaScript
<script>
var number = 2052021;
console.log(typeof number);
</script>
Output:
"number"
Example 3: When we check the type of undefined it is "undefined".
JavaScript
<script>
console.log(typeof undefined);
</script>
Output:
"undefined"
Example 4: The type of special symbol is "string".
HTML
<script>
var symbol = "@";
console.log(typeof symbol);
</script>
Output:
"string"
Example 5: The type of null is "object".
HTML
<script>
console.log(typeof null);
</script>
Output:
"object"
Example 6: The type of NaN ( not a number ) returns "number".
JavaScript
<script>
console.log(typeof NaN);
</script>
Output:
"number"
Example 7: In this example, a new object is created using a new keyword. typeof the variable created as "object".
JavaScript
<script>
let obj = new String("this is a string")
console.log(typeof obj);
</script>
Output:
"object"
Example 8: In the below example, a function is created to find the sum of two numbers and is passed on to a variable. type of the function variable is "function".
JavaScript
<script>
let func_object = function (a, b) {
return a + b;
};
console.log(typeof func_object);
</script>
Output:
"function"
Similar Reads
How to return the data type of variable in JavaScript ? To return the JavaScript data type of a variable we can use the JavaScript typeof operator. In JavaScript, unlike many other programming languages, we do not specify the type of a variable while declaring it, rather the variable's type is automatically inferred based on the value it holds. In other
3 min read
How to get the native type of a value in JavaScript ? JavaScript variables can store any type of value. The JavaScript typeof operator can be used to find out the native type of value. It returns a string value indicating the type of the value. Syntax: typeof(value) Possible output values of typeof: TypeResultundefined"undefined"null"object"boolean"boo
1 min read
How to Check the Data Type of a Variable in Ruby? In this article, we will discuss how to check the data type of a variable in Ruby. We can check the data types of a variable through different methods ranging from class to kind_of method in Ruby. Table of Content Using Class method Using is_a? Method Using kind_of? MethodUsing Class method The Clas
3 min read
How to check if a Variable Is Not Null in JavaScript ? In JavaScript, checking if a variable is not null ensures that the variable has been assigned a value and is not empty or uninitialized. This check is important for avoiding errors when accessing or manipulating data, ensuring that the variable holds valid, usable content before proceeding with oper
4 min read
How to Check the Type of an Object in Typescript ? When working with TypeScript, understanding how to check the type of an object is crucial for ensuring type safety and maintaining code integrity. TypeScript, being a statically typed superset of JavaScript, provides several approaches to accomplish this task as listed below.Table of ContentUsing th
3 min read