Course contentsShow
← JavaScript
Lesson 2329 of 2,72348. Working with Binary DataFree lesson

Byte Order and Endianness

How multi-byte values are stored and why endianness matters for typed arrays.

Byte Order and Endianness

You'll learn how multi-byte values are stored in memory and why this matters when working with typed arrays.

What is Endianness?

When a value larger than one byte (like a 16-bit integer or 32-bit float) is stored in memory, its bytes must be arranged in some order. Endianness refers to the order in which these bytes are stored.

There are two main conventions:

  • Big-endian: Most significant byte first (like reading left-to-right in English)
  • Little-endian: Least significant byte first (reversed order)

The Analogy

Imagine writing the number 1,234 on paper. Big-endian is like writing "1234" (thousands first). Little-endian is like writing "4321" (ones first). Both represent the same value, just stored differently.

Why It Matters for Typed Arrays

When you create a typed array from an ArrayBuffer, JavaScript reads the bytes according to your platform's native endianness (usually little-endian on modern systems). If you're reading binary data from a file or network that uses a different endianness, you'll get incorrect values.

For example, the 16-bit number 0x1234 stored in two bytes:

  • Big-endian: [0x12, 0x34]
  • Little-endian: [0x34, 0x12]

If you create a Uint16Array from these bytes on a little-endian system, the first arrangement would be read as 0x3412 (13,330 instead of 4,660)!

This becomes critical when exchanging binary data between systems or reading file formats with specific endianness requirements.

Key Takeaway: Endianness determines byte order for multi-byte values in typed arrays; mismatches between data format and platform can cause incorrect value interpretation.