Is there a Boolean Typed Array in JavaScript?



In this tutorial, we will learn if there is a Boolean Typed Array in JavaScript.

Typed Arrays are the objects same as arrays by using we can read or write raw binary data. It is useful to carry advanced information that contains lots of data like audio or videos fast and without distortion compared to our traditional way. The machine can only understand 0s and 1s. The typed arrays saved the information in an array of binary data. So, it becomes easier to transport the data from one end to another.

Now come to the question- Is there a Boolean Typed Array in JavaScript?

No, Boolean Typed Array does not exist. JavaScript supports the following typed arrays −

  • Int8Array
  • Uint8Array
  • Uint8ClampedArray
  • Int16Array
  • Uint16Array
  • Int32Array
  • Uint32Array
  • Float32Array
  • Float64Array

Above, you can see that the typed arrays are only available for integers and floats of various sizes. So, The Boolean Typed Arrays are not available in JavaScript. But, we can convert the boolean values into binary data using the TypedArray objects.

So, let us look at how we can convert the boolean values into binary data in JavaScript.

Following are the ways we can convert the boolean values into binary data in JavaScript

  • Using the Int8Array method

  • Using the ArrayBuffer object

Using the Int8Array() Method

The Int8Array is a typed array that is a representation of 8-bit signed integers in a binary form. It ranges from integer -128 to 127, and every element takes 1 byte of space.

Users can follow the below syntax to use the Int8Array method of the Typed array object.

Syntax

var bool = [  ];
var typedArr = new Int8Array( bool );

Follow the above syntax to convert boolean values into binary data.

Example

In the example below, we have created an array of boolean values. We passed this array as an argument to the Int8Array method. It returns the new array in which the true value is converted to 1 and the false is 0. Then we have to find the memory space taken by each element in an array by using the BYTES_PER_ELEMENT property.

<html> <body> <h2> Using the <i> Int8Array() </i> method to convert the boolean values into a binary data </h2> <div id="div"> </div> <script> var array = [true, false, true, true, false, true]; var para = document.createElement('p'); para.innerHTML = "Elements in an array: " + array; // para.style.color = "red"; document.getElementById("div").appendChild(para); var typedArr = new Int8Array(array); var para = document.createElement('p'); para.innerHTML = "Array with a binary representation: " + typedArr + "<br> Space taken by every element:" + typedArr.BYTES_PER_ELEMENT; // para.style.color = "red"; document.getElementById("div").appendChild(para); </script> </body> </html>

In the above example, users can see that we have used the Int8Array() method to convert the boolean values into binary data.

Using the ArrayBuffer object

We can call ArrayBuffer a container for storing raw binary data. We have to use the Dataview object or Typed arrays to access the content inside this object. These objects are used to process the ArrayBuffers.

Users can follow the below syntax to use the ArrayBuffer object in JavaScript.

Syntax

var variable1 = new ArrayBuffer(16);
var variable2 = new Int8Array(variable1);
variable2[0] = <True or false> ;
//print variable2[0]

Follow the above syntax to convert boolean values into binary data in JavaScript.

Example

In the example below, we have used the ArrayBuffer to create raw binary data. We have used the Typed Arrays to access the Buffers. We added the boolean values to the arrays, and after printing the array, we got the binary values.

<html> <body> <h2> Using the <i> ArrayBuffer object </i> to convert the boolean values into a binary data </h2> <div id="div"> </div> <script> var buffer = new ArrayBuffer(16); var binary = new Int8Array(buffer); binary[0] = true; binary[1] = false; var para = document.createElement('p'); para.innerHTML = "representation for true: " + binary[0].toString() + "<br> representation for false: " + binary[1].toString(); para.style.color = "red"; document.getElementById("div").appendChild(para); </script> </body> </html>

In the above example, users can see that by using ArrayBuffer and Typed Arrays, we have converted the boolean values into binary data. In this tutorial, we have learned there is no Boolean Typed Array in JavaScript. But, we can still convert boolean values to binary data.


Advertisements