Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Node.js util.types.isArrayBufferView() Method

  • Last Updated : 13 Aug, 2020

The “util” module provides ‘utility’ functions that are used for debugging purposes. For accessing those functions we need to call them (by ‘require(‘util’)‘).

The util.types.isArrayBufferView() (Added in v10.0.0) method is an inbuilt application programming interface of the util module which is equivalent to ArrayBuffer.isView() method. It checks whether the value is an instance of one of the typed array objects or DataView that is the ArrayBuffer view. If the value passed is typed array objects or DataView i.e, ArrayBuffer then it returns ‘true’ otherwise returns ‘false’.

Syntax:

const util = require('util');
util.types.isArrayBufferView(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 Value<Boolean>: It returns a boolean value. If the value passed is typed array objects or DataView i.e, ArrayBuffer then it returns ‘true’ otherwise returns ‘false’.

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

Example 1: Filename: index.js




// Node.js program to demonstrate the 
// util.types.isArrayBufferView() method 
   
// Using require to access util module 
const util = require('util'); 
const {types} = require('util'); 
  
// Passing ArrayBuffer as parameter
console.log("1.>", util.types.isArrayBufferView(new ArrayBuffer()));  
// Returns false
  
// Passing Array with argument as parameter
console.log("2.>", util.types.isArrayBufferView([1, "a", "@", "18"]));  
// Returns false
  
// Passing Buffer as parameter
console.log("3.>"
util.types.isArrayBufferView(Buffer.from('hello world'))); 
// Returns true
  
// Passing Float64Array as parameter
console.log("4.>", util.types.isArrayBufferView(new Float64Array()));  
// Returns true
  
// Passing data as parameter
console.log("5.>", util.types.isArrayBufferView([1, 5, 8, 18]));  
// Returns false
  
// Passing Array Buffer wrapped in dataView as parameter
console.log("6.>"
util.types.isArrayBufferView(new DataView(new ArrayBuffer(16))));  
// Returns true
  
// Passing Int8Array as parameter
console.log("7.>", util.types.isArrayBufferView(new Int8Array(8)));  
// Returns true

Run index.js file using the following command:

node index.js

Output:

1.> false

2.> false

3.> true



4.> true

5.> false

6.> true

7.> true

Example 2: Filename: index.js




// Node.js program to demonstrate the 
// util.types.isArrayBufferView() method 
   
// Using require to access util module 
const util = require('util'); 
const {types} = require('util'); 
   
let value1 = Uint8Array.from([1, 2, 3, 4])
var akash = value1.subarray(0, 2)
var arrayBuffer = akash.buffer; 
var geeksforgeeks = new BigUint64Array(465);
  
// Passing converted arrayBuffer as parameter
console.log("1.>", util.types.isArrayBufferView(arrayBuffer));  
// Returns false
  
// Passing empty parameter
console.log("2.>", util.types.isArrayBufferView());  
// Returns false
  
// Passing Float32Array as parameter
console.log("3.>", util.types.isArrayBufferView(new Float32Array()));  
// Returns true
  
// Passing JSON as parameter
console.log("4.>"
util.types.isArrayBufferView({name:"satyam", value:"sahai"}));  
// Returns false
  
var amit = new Uint8Array(["alfa", 1]);
  
// Converting to Array buffer
var newArrayBuffer = amit.buffer; 
  
// Converting and passing array buffer as parameter
console.log("5.>", util.types.isArrayBufferView(newArrayBuffer));  
// Returns false
  
// Passing Uint8Array as parameter
console.log("6.>", types.isArrayBufferView(new Uint8Array()));  
// Returns true
  
// Passing and converting buffer as parameter
console.log("7.>", types.isArrayBufferView(geeksforgeeks.buffer));  
// Returns false
   
var var1 = new ArrayBuffer(); 
var var2 = new Int16Array(); 
   
// Calling util.types.isArrayBufferView() method 
if (util.types.isArrayBufferView(var1)) 
    console.log("Yes, the value have ArrayBuffer View."); 
else
    console.log("No, provided value do not have ArrayBuffer View."); 
   
// Calling util.types.isArrayBufferView() method 
if (util.types.isArrayBufferView(var2)) 
    console.log("Yes, the value have ArrayBuffer View."); 
else
    console.log("No, provided value do not have ArrayBuffer View"); 

Run index.js file using the following command:

node index.js

Output:

1.> false

2.> false

3.> true



4.> false

5.> false

6.> true

7.> false

No, provided value do not have ArrayBuffer View.

Yes, the value have ArrayBuffer View.

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




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!