➤ How to Code a Game
➤ Array Programs in Java
➤ Java Inline Thread Creation
➤ Java Custom Exception
➤ Hibernate vs JDBC
➤ Object Relational Mapping
➤ Check Oracle DB Size
➤ Check Oracle DB Version
➤ Generation of Computers
➤ XML Pros & Cons
➤ Git Analytics & Its Uses
➤ Top Skills for Cloud Professional
➤ How to Hire Best Candidates
➤ Scrum Master Roles & Work
➤ CyberSecurity in Python
➤ Protect from Cyber-Attack
➤ Solve App Development Challenges
➤ Top Chrome Extensions for Twitch Users
➤ Mistakes That Can Ruin Your Test Metric Program
Get Type Of Object in JavaScript | Determining the type of an object in JavaScript is a crucial aspect of programming, as it allows you to perform specific operations based on the type of data you are working with. There are several methods to determine the type of an object in JavaScript, but in this article, we will focus on two commonly used methods: typeof operator and Object.prototype.toString(). Also see:- How to Compare Two Objects in JavaScript
Get Type Of Object in JavaScript Using typeof Operator
The typeof operator is the simplest and most commonly used method to determine the type of an object in JavaScript. The operator returns a string indicating the type of the operand. Here’s an example:
let number = 10;
let string = "Hello World!";
let object = { name: "John", age: 30 };
let array = [1, 2, 3, 4, 5];
console.log(typeof number); // "number"
console.log(typeof string); // "string"
console.log(typeof object); // "object"
console.log(typeof array); // "object"
Output:-
number
string
object
object
In the example above, the typeof operator is used to determine the type of four different variables. The operator returns “number” for the variable number, “string” for the variable string, “object” for the variable object, and “object” for the variable array. It is important to note that typeof operator returns “object” for arrays in JavaScript.
Get Type Of Object in JavaScript Using Object.prototype.toString() Method
Another method to determine the type of an object in JavaScript is the Object.prototype.toString() method. This method returns a string representation of the object and its class. Here’s an example:
let number = 10;
let string = "Hello World!";
let object = { name: "John", age: 30 };
let array = [1, 2, 3, 4, 5];
console.log(Object.prototype.toString.call(number));
console.log(Object.prototype.toString.call(string));
console.log(Object.prototype.toString.call(object));
console.log(Object.prototype.toString.call(array));
Output:-
[ object Number ]
[ object String ]
[ object Object ]
[ object Array ]
In the example above, the Object.prototype.toString() method is used to determine the type of four different variables. The method returns “[object Number]” for the variable number, “[object String]” for the variable string, “[object Object]” for the variable object, and “[object Array]” for the variable array.
In conclusion, determining the type of an object in JavaScript is an important operation that can be accomplished in several ways. The typeof operator and Object.prototype.toString() method are two commonly used methods to determine the type of an object. Choose the method that best fits your needs and use it in your JavaScript code to ensure that you have the right information when making decisions based on the type of data you are working with.
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!