Syntax
const buffer = new ArrayBuffer(bytes);
const view = new Uint8Array(buffer);Examples
Creating a Typed Array
The simplest way to work with typed arrays - directly, without manually managing the buffer.
const numbers = new Uint8Array([10, 20, 30, 255]);
console.log(numbers[0]); // 10
console.log(numbers.length); // 4
numbers[0] = 300; // out of range for Uint8Array (0-255)
console.log(numbers[0]); // 44 - silently wraps around (300 - 256)Working with ArrayBuffer Directly
Creating a raw buffer and viewing it through different typed array lenses.
const buffer = new ArrayBuffer(4); // 4 bytes of raw memory
const view8 = new Uint8Array(buffer);
view8[0] = 255;
view8[1] = 1;
const view32 = new Uint32Array(buffer); // same buffer, viewed as one 32-bit number
console.log(view32[0]); // interprets those same 4 bytes differentlyFloat32Array for Numeric Data
A common use case: representing collections of floating-point numbers efficiently.
const positions = new Float32Array([1.5, 2.25, 3.75, 4.0]);
for (const value of positions) {
console.log(value);
}
// Typed arrays support familiar array methods too
console.log(positions.map(v => v * 2));Best practices
- Use typed arrays instead of regular arrays when working with binary data, files, or performance-critical numeric computation - they use less memory and are faster for these cases
- Pick the specific typed array (Int8, Uint16, Float64, etc.) that matches your actual data range and precision needs
- Remember typed array values silently wrap or truncate when out of range, rather than throwing an error - validate input if that matters
- Reach for regular arrays for everyday application code - typed arrays are a specialized tool for binary/numeric-heavy scenarios
At a glance
- Purpose
- Application logic and interaction
- File extension
- .js ยท .mjs
- Runs in
- Browsers and JavaScript runtimes
- Usually used with
- HTML, CSS and Web APIs
Specifications & further reading
Related JavaScript documentation
Arrow Functions
Arrow functions provide a more concise syntax for writing function expressions in JavaScript. Introduced in ES6, they use the => syntax and have some important differences from regular functions, particularly in how they handle the "this" keyword. Arrow functions are especially useful for callbacks, array methods, and short inline functions.Variables: var, let, const
JavaScript has three ways to declare variables. var is the original way, function-scoped and hoisted with a default value of undefined. let, introduced in ES6, is block-scoped and can be reassigned. const is also block-scoped but cannot be reassigned after its initial value is set. Modern JavaScript strongly favors let and const over var.Data Types
JavaScript has seven primitive data types - string, number, boolean, undefined, null, bigint, and symbol - plus the object type, which includes arrays, functions, and plain objects. JavaScript is dynamically typed, meaning a variable can hold any type and that type can change. The typeof operator reports a value's type at runtime.Arithmetic Operators
Arithmetic operators perform mathematical calculations on numbers: addition (+), subtraction (-), multiplication (*), division (/), remainder/modulo (%), and exponentiation (**). The + operator also performs string concatenation when either operand is a string. Increment (++) and decrement (--) adjust a variable by one.
Arrow functions provide a more concise syntax for writing function expressions in JavaScript. Introduced in ES6, they use the => syntax and have some important differences from regular functions, particularly in how they handle the "this" keyword. Arrow functions are especially useful for callbacks, array methods, and short inline functions.Variables: var, let, const
JavaScript has three ways to declare variables. var is the original way, function-scoped and hoisted with a default value of undefined. let, introduced in ES6, is block-scoped and can be reassigned. const is also block-scoped but cannot be reassigned after its initial value is set. Modern JavaScript strongly favors let and const over var.Data Types
JavaScript has seven primitive data types - string, number, boolean, undefined, null, bigint, and symbol - plus the object type, which includes arrays, functions, and plain objects. JavaScript is dynamically typed, meaning a variable can hold any type and that type can change. The typeof operator reports a value's type at runtime.Arithmetic Operators
Arithmetic operators perform mathematical calculations on numbers: addition (+), subtraction (-), multiplication (*), division (/), remainder/modulo (%), and exponentiation (**). The + operator also performs string concatenation when either operand is a string. Increment (++) and decrement (--) adjust a variable by one.