Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

javascript
communitycreator

What is DataView.setFloat64() 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.setFloat64() is a method in JavaScript that takes a signed 64-bit float (double) value and sets this value at the specified byte offset from the start of the DataView object.

Syntax

The syntax of the method is as follows:


setFloat64(byteOffset : Number, value : Number, [littleEndian = false : Boolean]): void

Parameters

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

  • value: A signed 64-bit float (double) that is stored at the specified byte offset.

  • littleEndian: An optional parameter that specifies whether the 64-bit float is stored in little-endian or big-endian format. The value is False by default, 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 ArrayBuffer, which stores a reference to a fixed-length contiguous memory space that contains a raw sequence of bytes.

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

View objects

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

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

We can use DataView objects to access the data in any format we like and at any offset. Moreover, the format in which data is interpreted is done 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 ArrayBuffer. The default value is 0.

  • byteLength: The byte length that will be viewed from the specified byteOffset. The whole buffer is viewed by default.

Now that we have some idea of binary data in JavaScript and how to declare DataView objects, let’s look at how can we use dataView.setFloat32() in a program.

Code

const buffer = new ArrayBuffer(16);
const view = new DataView(buffer);
view.setFloat64(1, Math.E);
console.log(view.getFloat64(1));

Explanation

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

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

  • We use the dataView.setFloat64(1,Math.E) function to store the value of Euler’s Constant in the 32 bits after the first byte from the start of the view.

  • We use the dataView.setFloat64(1) method to verify that the value has been successfully set in the buffer.

RELATED TAGS

javascript
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