Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
ArrayBuffer.isView() function in JavaScript
The ArrayBuffer.isView() static method determines whether a given value is an ArrayBuffer view. ArrayBuffer views include typed arrays (like Int32Array, Float64Array) and DataView objects.
Syntax
ArrayBuffer.isView(value)
Parameters
value: The value to test whether it's an ArrayBuffer view.
Return Value
Returns true if the value is an ArrayBuffer view (typed array or DataView), false otherwise.
Example 1: Testing Typed Arrays
<html>
<head>
<title>ArrayBuffer.isView() Example</title>
</head>
<body>
<script>
// Create typed arrays
const int32Array = new Int32Array([1, 2, 3]);
const float64Array = new Float64Array([1.5, 2.7]);
const uint8Array = new Uint8Array([10, 20, 30]);
console.log("Int32Array:", ArrayBuffer.isView(int32Array));
console.log("Float64Array:", ArrayBuffer.isView(float64Array));
console.log("Uint8Array:", ArrayBuffer.isView(uint8Array));
</script>
</body>
</html>
Int32Array: true Float64Array: true Uint8Array: true
Example 2: Testing DataView and Non-Views
<html>
<head>
<title>ArrayBuffer.isView() - DataView Test</title>
</head>
<body>
<script>
const buffer = new ArrayBuffer(16);
const dataView = new DataView(buffer);
const regularArray = [1, 2, 3];
const plainObject = {a: 1, b: 2};
console.log("DataView:", ArrayBuffer.isView(dataView));
console.log("Regular Array:", ArrayBuffer.isView(regularArray));
console.log("Plain Object:", ArrayBuffer.isView(plainObject));
console.log("null:", ArrayBuffer.isView(null));
console.log("undefined:", ArrayBuffer.isView(undefined));
console.log("ArrayBuffer:", ArrayBuffer.isView(buffer));
</script>
</body>
</html>
DataView: true Regular Array: false Plain Object: false null: false undefined: false ArrayBuffer: false
What Are ArrayBuffer Views?
ArrayBuffer views are objects that provide a way to read and write data in an ArrayBuffer:
- Typed Arrays: Int8Array, Uint8Array, Int16Array, Uint16Array, Int32Array, Uint32Array, Float32Array, Float64Array
- DataView: Provides a low-level interface for reading and writing multiple number types
Common Use Cases
Use ArrayBuffer.isView() to validate parameters in functions that work with binary data or to differentiate between regular arrays and typed arrays.
Conclusion
ArrayBuffer.isView() is essential for checking if a value is a typed array or DataView. It returns true only for ArrayBuffer views, not for regular arrays or the ArrayBuffer itself.
