To check if a Variable is of type Error in JavaScript we have tested effectively with 3 methods: the instanceof() operator, using Object.prototype.toString, and using constructor property. Check out the details below to get more information.
Check if a Variable is of type Error in JavaScript
Using instanceof()
The instanceof operator tests the presence of the constructor in the object’s prototype chain. This usually means the object was constructed with a constructor.
Syntax: object instanceof constructor
Parameters:
- Object: The object to test.
- Constructor: Constructor to test against.
Return value: The return value is a boolean value.
Example:
var objError = new Error('Type is Error');
var mySite = "LearnShareIT";
console.log(objError instanceof Error) // true
console.log(mySite instanceof Error) // false
Output:
true
flase
Using Object.prototype.toString
To check if a variable is an Error object, you can use Object.prototype.toString
.
Object.prototype.toString() returns “[object Type]”, with Type is the object type.
To use the base Object.prototype.toString() with an object that has it undefined, you need to call Function.prototype.call()
Take a look at the example below, and the results returned
function checkVarTypeError(varObj){
//casts the return type as an error object by using === (Triple equals)
return Object.prototype.toString.call(varObj) === "[object Error]"; //here Type is Error
}
console.log("Error:", checkVarTypeError(new Error())); // object Error.
console.log("SyntaxError:", checkVarTypeError(new SyntaxError())); //SyntaxError() also a object Error
console.log("String:", checkVarTypeError("not Error"));
Result
Error: true
SyntaxError: true
String: false
Using constructor property
The constructor property returns a reference to the object constructor that created the instance using the assignment methods.
Take a look at the example below, and the results returned
function checkVarTypeError(varObj){
// Check if type of varObj is Error
return varObj.constructor === Error;
}
console.log("Error:", checkVarTypeError(new Error)); // only type is Error is return true
console.log("SyntaxError:", checkVarTypeError(new SyntaxError)); // false
console.log("String:", checkVarTypeError("Not Error")); // false
Result
Error: true
SyntaxError: false
String: false
Summary
In this tutorial, we have explained how to check if a Variable is of type error in JavaScript by using 3 methods, the instanceof() operator, using Object.prototype.toString, and using constructor property. We always hope this tutorial is helpful to you. Leave your comment here if you have any questions or comments to improve the article. Thanks for your read.
Maybe you are interested:
- If variable is Null or Undefined in JavaScript
- Check if Variable is of Function Type using JavaScript
- Check if a Variable is Not NULL in JavaScript
My name is Fred Hall. My hobby is studying programming languages, which I would like to share with you. Please do not hesitate to contact me if you are having problems learning the computer languages Java, JavaScript, C, C#, Perl, or Python. I will respond to all of your inquiries.
Name of the university: HUSC
Major: IT
Programming Languages: Java, JavaScript, C , C#, Perl, Python