How To Check Type in JavaScript - Javatpoint
How To Check Type in JavaScript - Javatpoint
Home Python Java JavaScript HTML SQL PHP C#
JavaScript Tutorial
JavaScript Introduction
JavaScript Example
External JavaScript
JavaScript Sum
JavaScript Basics
JS Comment
JS Variable
JS Global Variable
JS Data Types
JS Operators
JS If Statement
JS Switch
JS Loop
JS Function
JS Function - apply()
https://www.javatpoint.com/how-to-check-type-in-javascript 1/24
10/15/24, 11:32 AM How to Check Type in JavaScript- Javatpoint
JS Function - bind()
JS Function - call()
JS Function - toString()
JavaScript Objects
← prev next →
let x = 5;
let y = "Hello";
let z = true;
console.log(typeof x);
console.log(typeof y);
console.log(typeof z);
https://www.javatpoint.com/how-to-check-type-in-javascript 2/24
10/15/24, 11:32 AM How to Check Type in JavaScript- Javatpoint
Output:
"number"
"string"
"boolean"
For typeof x equals "number" is that x has a value that is assigned to it as a number.
The instanceof operator is used to check if an object belongs to a particular class or not. It
categorizes the items in an object is identified as either of a certain class or its subclasses.
https://www.javatpoint.com/how-to-check-type-in-javascript 3/24
10/15/24, 11:32 AM How to Check Type in JavaScript- Javatpoint
Output:
True
true
This is because arr is an instance of the Array class; thus, the instanceof Array evaluates to
true.
Because obj belongs to the Object class, obj instanceof Object gives true as a result.
The method Array.isArray(obj) is not applicable here because obj is not an array, so it returns
false.
https://www.javatpoint.com/how-to-check-type-in-javascript 4/24
10/15/24, 11:32 AM How to Check Type in JavaScript- Javatpoint
let num = 5;
let str = "Hello";
let bool = true;
let func = function() {};
let arr = [1, 2, 3];
let obj = { name: "John", age: 30 };
console.log(Object.prototype.toString.call(num));
console.log(Object.prototype.toString.call(str));
console.log(Object.prototype.toString.call(bool));
console.log(Object.prototype.toString.call(func));
console.log(Object.prototype.toString.call(arr));
console.log(Object.prototype.toString.call(obj));
Output:
[object Number]
[object String]
[object Boolean]
[object Function]
[object Array]
[object Object]
Explaination:
https://www.javatpoint.com/how-to-check-type-in-javascript 5/24
10/15/24, 11:32 AM How to Check Type in JavaScript- Javatpoint
Developers can now be able to accurately find the type of variables or values in their
JavaScript applications by employing methods like typeof, instanceof, Array.isArray(), and
Object.prototype.toString.call(). It ensures that there will be no risk of mistakes and
incorrect manipulation.
Advertisement
https://www.javatpoint.com/how-to-check-type-in-javascript 6/24
10/15/24, 11:32 AM How to Check Type in JavaScript- Javatpoint
The function type of the variable func is shown by the return value "function" from typeof
func.
JavaScript may return NaN (Not a Number) when working with numeric numbers,
especially when performing operations like division by zero or improper mathematical
operations. The isNaN() method can be used to check for NaN.
let result1 = 10 / 0;
let result2 = "Hello" / 5;
Knowing the types of JavaScript methods for type checking used by developers makes
them powerful enough to make sure their code runs as expected. Being able to use typeof
https://www.javatpoint.com/how-to-check-type-in-javascript 7/24
10/15/24, 11:32 AM How to Check Type in JavaScript- Javatpoint
for basic type checking, instanceof for object type checking, and Array.isArray() for arrays
will be useful for developers to build more reliable and reusable JavaScript programs.
Besides, having the detailed and odd features - like typeof null returns "object" or use
isNaN() to manage NaN - gives us the ability to navigate the more skillfully and fluently the
type system. Developers may avoid errors and contribute to better code integrity by
following the suggested techniques and employing them in their coding practices.
let x;
let y;
https://www.javatpoint.com/how-to-check-type-in-javascript 8/24
10/15/24, 11:32 AM How to Check Type in JavaScript- Javatpoint
Reason: As x and y are both declared but not defined, typeof returns "undefined" for both of
them.
For instance:
Justification: A person's "name" will be assigned to a person object if the "name" attribute
is available.
"gender" in the "person" is the object that helps to identify the "gender" property.
Whether the person object's "gender" property is set or not is tested by checking the
presence of a person.hasOwnProperty("gender").
function greet() {
console.log("Hello!");
}
Output:
True
false
https://www.javatpoint.com/how-to-check-type-in-javascript 10/24
10/15/24, 11:32 AM How to Check Type in JavaScript- Javatpoint
This is because since welcome is a function, typeof greet === "function" yields true.
Conclusion
Writing dependable code in JavaScript requires an understanding of type-checking. To
reliably establish types, JavaScript has several techniques, including typeof, instanceof, and
object property checks. These may be used with primitive types, objects, or functions. By
utilizing these strategies efficiently, developers may build cleaner, more maintainable code
that handles various data types correctly. This improves developer productivity and
software quality.
← prev next →
https://www.javatpoint.com/how-to-check-type-in-javascript 11/24
10/15/24, 11:32 AM How to Check Type in JavaScript- Javatpoint
https://www.javatpoint.com/how-to-check-type-in-javascript 12/24
10/15/24, 11:32 AM How to Check Type in JavaScript- Javatpoint
https://www.javatpoint.com/how-to-check-type-in-javascript 13/24
10/15/24, 11:32 AM How to Check Type in JavaScript- Javatpoint
https://www.javatpoint.com/how-to-check-type-in-javascript 14/24
10/15/24, 11:32 AM How to Check Type in JavaScript- Javatpoint
https://www.javatpoint.com/how-to-check-type-in-javascript 15/24
10/15/24, 11:32 AM How to Check Type in JavaScript- Javatpoint
https://www.javatpoint.com/how-to-check-type-in-javascript 16/24
10/15/24, 11:32 AM How to Check Type in JavaScript- Javatpoint
https://www.javatpoint.com/how-to-check-type-in-javascript 17/24
10/15/24, 11:32 AM How to Check Type in JavaScript- Javatpoint
https://www.javatpoint.com/how-to-check-type-in-javascript 18/24
10/15/24, 11:32 AM How to Check Type in JavaScript- Javatpoint
https://www.javatpoint.com/how-to-check-type-in-javascript 19/24
10/15/24, 11:32 AM How to Check Type in JavaScript- Javatpoint
https://www.javatpoint.com/how-to-check-type-in-javascript 20/24
10/15/24, 11:32 AM How to Check Type in JavaScript- Javatpoint
https://www.javatpoint.com/how-to-check-type-in-javascript 21/24
10/15/24, 11:32 AM How to Check Type in JavaScript- Javatpoint
Latest Courses
https://www.javatpoint.com/how-to-check-type-in-javascript 22/24
10/15/24, 11:32 AM How to Check Type in JavaScript- Javatpoint
https://www.javatpoint.com/how-to-check-type-in-javascript 23/24
10/15/24, 11:32 AM How to Check Type in JavaScript- Javatpoint
https://www.javatpoint.com/how-to-check-type-in-javascript 24/24