Warning: session_start(): open(/tmp/sess_542d4532fd5d10841707ebe253fd07fd, O_RDWR) failed: Disk quota exceeded (122) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: session_start(): Failed to read session data: files (path: /tmp) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_mkdir() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 562

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230
How To Check if a Variable is of type Error in JavaScript - LearnShareIT

How To Check if a Variable is of type Error in JavaScript

check if a Variable is of type Error in JavaScript

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:

Leave a Reply

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