Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

javascript
arraybuffer
communitycreator

What is DataView.getUint8() in JavaScript?

Abdul Monum

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

DataView.prototype.getUint32() is a method in JavaScript that gets an unsigned 8-bit integer value at the specified byte offset from the DataView object’s start.

Syntax

The syntax of the method is as follows.


getUint8(byteOffset : Number, [littleEndian = false : Boolean]) : Number

Parameters

  • byteOffset: A Number that specifies the byte offset from the start of the view.

  • littleEndian: An optional parameter that specifies whether the 8-bit integer that is retrieved should be interpreted in the little-endian format or the big-endian format.

By default, the value is False, which means a big-endian value is stored.

Binary data in JavaScript and the DataView class

While many languages provide byte arrays to manipulate binary data, things work differently in JavaScript.

The most fundamental binary object in JavaScript is the ArrayBuffer, which stores a reference to a fixed-length contiguous memory space that contains a raw sequence of bytes.

However, to interpret and manipulate the ArrayBuffer, we need something called view objects.

View objects

View objects themselves do not store any data but instead provide different interfaces to read and write byte data in the raw buffer.

The DataView class is one such view over the ArrayBuffer that provides flexibility in interpreting the buffer.

We can access the data in any format we like and at any offset by using DataView objects. The format in which data is interpreted is determined at the method call time.

Syntax of DataView

The syntax to declare a DataView object is as follows.


new DataView(buffer, [byteOffset], [byteLength])

  • buffer: The ArrayBuffer object that contains the raw sequence of bytes.

  • byteOffset: The starting byte position of the view from the starting position of the ArrayBuffer. The default value is 0.

  • byteLength: The byte length to be viewed from the specified byteOffset. By default, the whole buffer is viewed.

Now, we have an idea of binary data in JavaScript and how to declare DataView objects.

Let’s look at how we can use dataView.getUint8() in a program.

Code

buffer = new ArrayBuffer(12);
let dataView = new DataView(buffer);
dataView.setUint8(1,255)
console.log(dataView.getUint8(1))

Explanation

  • In the example above, we declare an ArrayBuffer with a length of 12 bytes.

  • We create the object dataView using the constructor of the DataView class and pass the buffer object to it.

  • We store the value 255 in the 8 bits after the first byte from the start of the view using the dataView.setUint8(1, 255) function call.

  • We verify that the value has been set successfully in the buffer using the dataView.getUint8(1) method.

RELATED TAGS

javascript
arraybuffer
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring