Science Knowings: JavaScript Course For Social Media

Typed Arrays

Typed Arrays: Supercharged Arrays for Binary Data

Welcome to the world of Typed Arrays! These specialized arrays elevate JavaScript's data handling capabilities, empowering you to store and manipulate binary data with unmatched efficiency.

What are Typed Arrays?

Typed Arrays are specialized array-like objects that store binary data in a contiguous memory block. Unlike regular arrays, they enforce a specific data type (e.g., integers, floats) for all elements.

Creating Typed Arrays

To create a Typed Array, use the constructor syntax: var myTypedArray = new TypedArray(length or buffer), where length specifies the number of elements, and buffer is an existing buffer (ArrayBuffer) to wrap.

Int8Array

Int8Array stores 8-bit signed integers. It's suitable for storing small negative and positive whole numbers (range: -128 to 127).

Uint8Array

Uint8Array stores 8-bit unsigned integers. Unlike Int8Array, it can only represent non-negative whole numbers (range: 0 to 255).

Int16Array

Int16Array stores 16-bit signed integers, expanding the range of Int8Array (range: -32,768 to 32,767).

Uint16Array

Uint16Array stores 16-bit unsigned integers, offering a wider range for non-negative whole numbers (range: 0 to 65,535).

Int32Array

Int32Array stores 32-bit signed integers, providing an even greater range (range: -2,147,483,648 to 2,147,483,647).

Uint32Array

Uint32Array stores 32-bit unsigned integers, allowing for a massive range of non-negative whole numbers (range: 0 to 4,294,967,295).

Float32Array

Float32Array stores 32-bit floating-point numbers, enabling the representation of decimal values with precision.

Float64Array

Float64Array stores 64-bit floating-point numbers, providing even higher precision for decimal values.

DataView

DataView allows you to interpret an ArrayBuffer as a view of a different data type, offering flexibility in data representation.

Typed Array Methods

Typed Arrays come with a range of useful methods:

  • set() - Sets the values of the array
  • subarray() - Creates a new Typed Array from a subset of the original
  • buffer - Returns the underlying ArrayBuffer
  • byteLength - Returns the length of the Typed Array in bytes
  • byteOffset - Returns the offset of the Typed Array within the ArrayBuffer

ArrayBuffer Object: Introduction

In our next session, we'll delve into ArrayBuffer, the foundation for Typed Arrays. Get ready to understand how you can work with raw, binary data efficiently. Follow us to stay tuned!