Open In App

Node.js | util.types.isDataView() Method

Last Updated : 14 Feb, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

The util.types.isDataView() method method of util module is primarily designed to support the needs of Node.js own Internal APIs. 
The util.types.isDataView() method is used to check if the given value is a DataView object or not.
 

Syntax: 

util.types.isDataView(value)

Parameters: This function accepts one parameter as mentioned above and described below: 

  • value: It is the value that would be checked for a DataView object.

Return Value: This method returns a Boolean value i.e. true if the passed value is instance of DataView otherwise returns false.
 

Below programs illustrate the util.types.isDataView() method in Node.js:
 

Example 1:

Node.js




// Node.js program to demonstrate the    
// util.types.isDataView() method 
  
// Import the util module
const util = require('util');
  
// Checking for a DataView
let arraybuf = new ArrayBuffer(5);
let dataView = new DataView(arraybuf);
console.log("Value:", dataView);
  
isDataView = util.types.isDataView(dataView);
console.log("Value is a DataView:", isDataView);
  
// Checking for an Integer array
let intArray = new Int32Array();
console.log("Value:", intArray);
  
isDataView = util.types.isDataView(intArray);
console.log("Value is a DataView:", isDataView);


Output:

Value: DataView {
  byteLength: 5,
  byteOffset: 0,
  buffer: ArrayBuffer { [Uint8Contents]:
  <00 00 00 00 00>, byteLength: 5 }
}
Value is a DataView: true
Value: Int32Array []
Value is a DataView: false

Example 2:
 

Node.js




// Node.js program to demonstrate the    
// util.types.isDataView() method 
  
// Import the util module
const util = require('util');
  
// Creating an unsigned integer 8-bit array
let int8Array = new Uint8Array([10, 20, 30]);
console.log("Value:", int8Array);
  
// Checking for a view
console.log("Value is a View:"
      ArrayBuffer.isView(int8Array));
  
// Checking for a DataView
isDataView = util.types.isDataView(int8Array);
console.log("Value is a DataView:", isDataView);
  
// Creating a dataview
let dataView = new DataView(new ArrayBuffer(3));
dataView.setUint8(0, 10);
dataView.setUint8(1, 20);
dataView.setUint8(2, 30);
console.log("Value:", dataView);
  
// Checking for a view
console.log("Value is a View:",
      ArrayBuffer.isView(dataView));
  
// Checking for a DataView
isDataView = util.types.isDataView(dataView);
console.log("Value is a DataView:", isDataView);


Output:

Value: Uint8Array [ 10, 20, 30 ]
Value is a View: true
Value is a DataView: false
Value: DataView {
  byteLength: 3,
  byteOffset: 0,
  buffer: ArrayBuffer { [Uint8Contents]: 
  <0a 14 1e>, byteLength: 3 }
}
Value is a View: true
Value is a DataView: true

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



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads