Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Node.js util.types.isNativeError() Method

  • Last Updated : 07 Oct, 2021

The util.types.isNativeError() (Added in v10.0.0) method is an inbuilt application programming interface of the util module which is used to identify whether the value is an instance of a built-in Error type in the node.js. If the instance has an inbuilt ‘Error’ then it returns ‘true’ otherwise returns ‘false’.

Syntax:

util.types.isNativeError( value )

Parameters: This method accepts a single parameter as mentioned above and described below:

  • value <any>: It is a required parameter that accepts any variable, Class, Function, Object, or JavaScript primitive or any datatype.

Return <Boolean>: This returns a boolean value, If the instance has an inbuilt ‘Error’ then it returns ‘true’ otherwise returns ‘false’.

Below examples illustrate the use of util.types.isNativeError(value) method in Node.js.

Example 1: Filename: index.js




// Node.js program to demonstrate the    
// util.types.isNativeError() Method
  
// Import the util module 
const util = require('util');
const {types} = require('util');
  
// Passing no values => Returns false
console.log("1.>", util.types.isNativeError());  
  
// Passing new inbuilt Error => Returns true
console.log("2.>", util.types.isNativeError(new Error())); 
   
// Passing new inbuilt TypeError => Returns true
console.log("3.>", util.types.isNativeError(new TypeError())); 
   
// Passing new inbuilt Error => Returns true
console.log("4.>", types.isNativeError(new RangeError())); 
   
// Passing new inbuilt Error => Returns true
console.log("5.>", util.types.isNativeError(new SyntaxError()));
    
// Passing new inbuilt Error => Returns true
console.log("6.>", types.isNativeError(new ReferenceError()));  

Run index.js file using the following command:

node index.js

Output:

1.> false
2.> true
3.> true
4.> true
5.> true
6.> true

Example 2: Filename: index.js




// Node.js program to demonstrate the    
// util.types.isNativeError() Method
  
// Import the util module 
const util = require('util');
const {types} = require('util');
  
// Directly importing and calling types
console.log("1.>", types.isNativeError(new Error("ksdjfhks")));
  
// Passing data/values directly
console.log("2.>", util.types.isNativeError(1234+'45'));  
  
// Passing Error.stack directly
console.log("3.>", util.types.isNativeError(Error.stack));  
  
// Passing util.types.isNativeError() method directly
console.log("4.>"
util.types.isNativeError(util.types.isNativeError(new Error())));
  
// Passing util.types.isNativeError() method with 'Error'
console.log("5.>"
util.types.isNativeError(util.types.isNativeError(Error)));
  
// Creating object
const object1 = {};
Error.captureStackTrace(object1);
  
// Passing object
console.log("6.>", util.types.isNativeError(object1.stack)); 
  
// Passing Error.captureStackTrace(object1)
console.log("7.>"
util.types.isNativeError(Error.captureStackTrace(object1))); 
  
// Passing util.types.isNativeError() method with
/// nested error message
console.log("8.>"
util.types.isNativeError(util.types.isNativeError(new Error())));
  
// Creating new error
const errorr = new Error('Some Error message');
  
// Passing new Error indirectly 
console.log("9.>", util.types.isNativeError(errorr)); 
  
// Passing error message 
console.log("10.>", util.types.isNativeError(errorr.message)); 
  
// Printing error
console.log("11.>", util.types.isNativeError(
        console.error(errorr.message))); 

Run index.js file using the following command:

node index.js

Output:

1.> true
2.> false
3.> false
4.> false
5.> false
6.> false
7.> false
8.> false
9.> true
10.> false
Some Error message
11.> false

Reference: https://nodejs.org/api/util.html#util_util_types_isnativeerror_value 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!