Updated April 6, 2023
Introduction to TypeScript instanceof
The Typescript instanceof is one of the operators, and it is used to determine the specific constructor, and it will be creating the object of the classes. It will call the methods with the help of instance like that if we use an interface that can be implemented and extended through the classes. It will determine the property, and its chain will be added to the object it checks the actual condition of the prototype. It returns the Boolean conditions also the name of the variables on its left-hand side included the function of the classes.
Syntax
The instanceof operator is the keyword, and it is mainly used for the object to validate it’s a type or not, and it is followed up through the constructor function. The keyword uses and focuses on the class areas also, and it can be accessed and utilized in the variables, conditional statements, loops, etc.
function functionName(reference: class)
{
conditional statements(reference instanceof another class)
{
----- some logics depends upon the user requirements --------
}
}
The above code is the basic syntax for utilizing the instanceof operator in the typescript classes. Using that instance, the methods are called for the specific class, which has been already declared on the class. Even the implementation of the interface is also being referred to the operator with the conditional statements.
How instanceof works in TypeScript?
In Typescript instanceof is one of the operators, and it takes the name of the variables on its left-hand side also the name of the function or class; the interface also initialized on the left side of the class. The instanceof operator is used to validate the objects at runtime whenever we want to create the objects at the specific classes; also, it always returns the boolean values like true or false statements if the required class need to create the instance and it requires to authenticate with the other code logics. Basically, the instanceof is the advanced type of the typescript; meanwhile, the operator is tested to see if the prototype property for the class-based constructors appears anywhere in the objects’ prototype chain.
The different scopes have a different set of executions in run time environments; also, it accepts compile-time scripts if the validation is failed for the specific class instance, then it throws the error in the compile-time itself sometimes the operator belongs to execute with the runtime mode in that it throws exceptions on the output console. While we evaluate the class reference using conditional statements, it validates the actual class instance and new class reference.
Examples of TypeScript instanceof
Different examples are given below:
Example #1
function demo(exam, exam1, exam2) {
this.exam = exam;
this.exam1 = exam1;
this.exam2 = exam2;
}
const demo1 = new demo('first', 'second', 'third');
console.log(demo1 instanceof demo);
console.log(demo1 instanceof Object);
function sample(vars, vars1, vars2, vars3, vars4, vars5, vars6, vars7){
this.vars = vars;
this.vars1 = vars1;
this.vars2 = vars2;
this.vars3 = vars3;
this.vars4 = vars4;
this.vars5 = vars5;
this.vars6 = vars6;
this.vars7=vars7;
}
const demo2 = new sample('four fsh35 hasd has has', 'secondfive', 'six', 'hs','js','sd','sde','sad sd swf fg fd');
console.log(demo2 instanceof demo);
console.log(demo2 instanceof Object);
Output:
The above example shows the instanceof operator is used in one of the ways like that we can create the two methods like “demo” and “sample” for the two methods we can pass the set of arguments in each method. If we take the “demo()” method, it takes 3 arguments. In the same we can pass the values to the separate variable like that in the sample() method, multiple set of arguments are passing. Each argument value should be initialized with a separate variable type like “const” and creating a new object for the class using the “instanceof” operator; the class reference is validated and printed on the output console.
Example #2
interface demo {
example(): string;
}
class demo1 implements demo {
constructor(private ns: number) {}
example() {
return Array(this.ns + 1).join(" ");
}
}
class demo2 implements demo {
constructor(private vl: string) {}
example() {
return this.vl;
}
}
function examp() {
return Math.round(764.9834733746928374) < 10000000
? new demo1(79)
: new demo2(" ");
}
let pads: demo = examp();
if (pads instanceof demo1) {
console.log("Welcome To My Domain its a second example \n")
console.log(pads);
}
if (pads instanceof demo2) {
console.log("Welcome To My Domain its a second example \n")
console.log(pads)
}
Output:
In the second example, we use additional classes and interfaces. Normally the interface is used to declare the methods or functions; it can’t be implemented with the help of classes; the interface methods are implemented and printed on the output console. Here we called some math functions like the round() method for validating the mathematical values also it compares another number. By using if conditional statement and instanceof operator, the classes are validated its types.
Example #3
class demo {
vars;
constructor(vars) {
this.vars = vars;
}
}
const examp = new demo('Welcome To My Domain jwegd qiwdkgds kjqcgsd kjGCDS CWGKDS');
console.log(examp instanceof demo);
console.log(Object.getPrototypeOf(examp) === demo.prototype);
console.log(examp instanceof Object);
console.log(Object.getPrototypeOf(demo.prototype) === Object.prototype);
console.log(examp instanceof class name {
});
console.log(Object.getPrototypeOf(demo.prototype) === Object.prototype);
console.log(examp instanceof Function);
Output:
For the final example, we used the instanceof operator in the same ways as that already we used on the previous two examples. Additionally, we have used the operator with different ways of the console.log statement of the typescript like instanceof operator validate the types with Object, Function, other custom or user-defined method like “demo”. Here we also include the class name keyword for comparing the type using instanceof operator so that it creates the ‘{ and }’ open and close brackets for to implement the class name.
Rules and Regulations for instanceof
- The instanceof operator compares the class names, objects, functions and including other user-defined method types.
- It was also applicable for variables and other conditional statements for validating or comparing the two references.
Conclusion
The typescript language is generally used with many operators, keywords, and other pre-defined functions to implement the application in a more user-friendly nature. Like that instanceof operator will compare and validate the two object references, keywords, and other user-defined functions by using conditional statements it will be evaluated; finally, it returns the boolean value as the output console.
Recommended Articles
We hope that this EDUCBA information on “TypeScript instanceof” was beneficial to you. You can view EDUCBA’s recommended articles for more information.