Check If Object Is Of type Set In JavaScript

Check if Object is of type Set in JavaScript

We will find out how to check if object is of type set in JavaScript with two different approaches. Each approach is straightforward, and you can choose whichever you like.

Check if object is of type set in JavaScript

Using instanceof

The instanceof operator can be used to check if an instance (object) is of a specific type or not. This is a binary operator (which takes two operands) and non-associative. For example:

var set = new Set([1,2,3]);
var array = [4,5,6];

// Check if the variable set is of type set
console.log(set instanceof Set);

// Check if the variable array is of type set
console.log(array instanceof Set);

Output: 

true 
false

The above example uses the instanceof syntax to check if the object provided in the first operand has the type of the class declared in the second operand or not. This approach will return true even when the type you provided is a parent class of the real class to which the object belongs:

var set = new Set([1,2,3]);
var array = [4,5,6];

// Check if the variable set is of type set
console.log(set instanceof Object);

// Check if the variable array is of type set
console.log(array instanceof Object);

Output: 

true
true

The reason behind this example is straightforward. You might wonder why the method returns true even when the object is of type set or array. It is because every object in JavaScript is always inherited from the object class, so it means that the set type (or any type except primitive types) is the subclass of the object type. This logic might provide some unexpected results when the object type is not set but is a type of subclass of the set you declared before.

// Create a new class inherit from Set class and its methods
class MySet extends Set{
}
var mySet = new MySet([1,2,3]);
var realSet = new Set([1,2,3]);

// Check if object mySet is of type set
console.log(mySet instanceof Set);

// Check if object mySet is of type MySet
console.log(mySet instanceof MySet);

// Check if object realSet is of type MySet
console.log(realSet instanceof MySet);

// Check if object realSet is of type Set
console.log(realSet instanceof Set);

Output: 

true
true
false
true

As you can see, even the object mySet is not the instance of a set class, but this solution also provides true. This has nothing to do with the error behind the operator because this operator was born to help developers check the type of its parent classes. However, if this solution will cause conflicts with what you want, you could consider using our second solution in the following.

Using property constructor.name

The constructor.name is a property of any object in JavaScript; you can use this property to access the class name, which you can indicate its type from there:

// Create a new class inherit from Set class and its methods
class MySet extends Set{
}
var mySet = new MySet([1,2,3]);
var realSet = new Set([1,2,3]);

// Check if the variable set is of type set
console.log(mySet.constructor.name);

// Check if the variable array is of type set
console.log(realSet.constructor.name);

// Check if mySet is an object of type Set
console.log(mySet.constructor.name == "Set")

// Check if realSet is an object of type Set
console.log(realSet.constructor.name == "Set")

Output: 

MySet
Set
false
true

The example below shows that we have checked the type of the object using the constructor name, which equals the string representing the type of that object. The result is mySet is not of type set (because it is of type MySet) and realSet is of type set.

Summary

We have learned to check if object is of type set in JavaScript. It would help if you considered each method’s pros and cons. We hope you will be successful in achieving the task with our tutorials.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *