JavaScript Typed Arrays — Interview Questions & Answers
Ques: What are Typed Arrays in JavaScript?
Ans: Typed Arrays are array-like objects that allow you to read and write raw binary data directly from memory buffers. They are not the same as normal arrays — they are used for high-performance numerical and binary operations.
Example:
const buffer = new ArrayBuffer(8);
const view = new Int32Array(buffer);
view[0] = 42;
console.log(view[0]); // 42
Ques: Why were Typed Arrays introduced in JavaScript?
Ans: Typed Arrays were introduced to:
- Work with binary data directly.
- Improve performance for numeric calculations.
- Enable WebGL, WebAssembly, and streaming data.
- Support low-level operations like image and audio processing.
Ques: What are the main components of Typed Arrays?
Ans: There are two main parts:
- ArrayBuffer – a fixed-size memory area that stores raw bytes.
- TypedArray View – a specific view to interpret those bytes (like
Int8Array,Float32Array, etc.).
Example:
const buffer = new ArrayBuffer(16);
const int32 = new Int32Array(buffer);
Ques: What are some common Typed Array types?
| Typed Array | Bytes per Element | Range |
|---|---|---|
Int8Array | 1 | -128 to 127 |
Uint8Array | 1 | 0 to 255 |
Int16Array | 2 | -32,768 to 32,767 |
Uint16Array | 2 | 0 to 65,535 |
Int32Array | 4 | -2,147,483,648 to 2,147,483,647 |
Uint32Array | 4 | 0 to 4,294,967,295 |
Float32Array | 4 | Decimal values |
Float64Array | 8 | Higher precision decimals |
Ques: What is the difference between Array and TypedArray?
| Feature | Regular Array | Typed Array |
|---|---|---|
| Data Type | Any type (mixed) | Fixed numeric type |
| Size | Dynamic | Fixed |
| Performance | Slower | Faster (binary access) |
| Use Case | General JS data | Binary / numeric data |
| Memory | Not contiguous | Contiguous (better for performance) |
Ques: How do you create a Typed Array in JavaScript?
Ans: There are multiple ways:
// Create with length
const arr1 = new Uint8Array(4);
// From an array
const arr2 = new Float32Array([1.5, 2.5, 3.5]);
// From an ArrayBuffer
const buffer = new ArrayBuffer(8);
const arr3 = new Int16Array(buffer);
Ques: What is an ArrayBuffer?
Ans: ArrayBuffer is a fixed-length block of memory used to store raw binary data.
It cannot be accessed directly — you must use a TypedArray view or DataView.
Example:
const buffer = new ArrayBuffer(16);
console.log(buffer.byteLength); // 16 bytes
Ques: What is a DataView in JavaScript?
Ans: DataView provides more control over reading/writing data with different types and byte orders (endianness).
const buffer = new ArrayBuffer(8);
const view = new DataView(buffer);
view.setInt8(0, 100);
console.log(view.getInt8(0)); // 100
Ques: What are some useful Typed Array properties?
| Property | Description |
|---|---|
.buffer | References the underlying ArrayBuffer |
.byteLength | Number of bytes occupied |
.length | Number of elements |
.BYTES_PER_ELEMENT | Size per element in bytes |
Example:
const arr = new Int16Array(4);
console.log(arr.BYTES_PER_ELEMENT); // 2
Ques: What are common Typed Array methods?
| Method | Description |
|---|---|
.set() | Copy array values into another TypedArray |
.subarray(start, end) | Creates a new view of a part of the array |
.slice() | Copies a section |
.map(), .forEach(), .filter() | Work like Array methods |
.fill() | Fills array with a static value |
.reverse(), .sort() | Same as regular Array methods |
Example:
const arr = new Uint8Array([10, 20, 30]);
arr.set([40, 50], 1);
console.log(arr); // [10, 40, 50]
Ques: How can you convert a Typed Array to a normal Array?
const typed = new Float32Array([1.1, 2.2, 3.3]);
const normal = Array.from(typed);
console.log(normal); // [1.1, 2.2, 3.3]
Ques: What are Typed Array use cases in real-world applications?
- Handling binary file data (images, audio, PDFs)
- WebGL and 3D rendering
- Machine learning numeric calculations
- Video and game development
- Data compression/decompression
Ques: What is BYTES_PER_ELEMENT used for?
Ans: It tells how many bytes each element of the typed array uses.
console.log(Int32Array.BYTES_PER_ELEMENT); // 4
Ques: What is the role of byteOffset in Typed Arrays?
Ans: Specifies the starting point (in bytes) for the view inside the ArrayBuffer.
const buffer = new ArrayBuffer(8);
const view = new Int16Array(buffer, 2); // starts at byte 2
Ques: What is a WeakSet, and is it similar to Typed Arrays?
Ans: No, a WeakSet stores object references only and has no relation to Typed Arrays.
Typed Arrays work with binary numeric data, not objects.
Ques: How do you copy part of one Typed Array into another?
const a = new Uint8Array([1, 2, 3]);
const b = new Uint8Array(3);
b.set(a);
console.log(b); // [1, 2, 3]
Ques: Can you resize a Typed Array?
Ans: No, Typed Arrays have a fixed size.
If you need resizing — create a new Typed Array and copy values.
Ques: Are Typed Arrays iterable?
Ans: Yes, they can be looped using for...of.
const arr = new Int8Array([5, 10, 15]);
for (let value of arr) console.log(value);
Ques: How are Typed Arrays different from DataView?
| Feature | TypedArray | DataView |
|---|---|---|
| Purpose | Simple, uniform view | Fine-grained control |
| Type | Fixed (like Int8Array) | Custom per operation |
| Use Case | Fast numeric data | Parsing mixed binary data |
Ques: Can Typed Arrays handle floating-point numbers?
Ans: Yes, with Float32Array and Float64Array.
const floats = new Float32Array([1.5, 2.75, 3.9]);
console.log(floats[1]); // 2.75
Ques: What happens if you assign out-of-range values?
Ans: The values are clamped or wrapped according to type range.
const arr = new Int8Array(1);
arr[0] = 200; // wraps around
console.log(arr[0]); // -56
