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 array-like objects that allow reading and writing raw binary data efficiently. They are particularly useful for handling large amounts of data like audio, video, or image processing, as they provide faster data transport compared to regular arrays. Since machines work with binary data (0s and 1s), typed arrays store information in binary format, making data transfer more efficient.

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 typed arrays are only available for integers and floats of various sizes. Boolean Typed Arrays are not available in JavaScript. However, we can convert boolean values into binary data using existing TypedArray objects.

Following are the ways we can convert 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 representing 8-bit signed integers in binary form. It ranges from -128 to 127, with each element taking 1 byte of space.

Syntax

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

Example

In this example, we create an array of boolean values and pass it to the Int8Array constructor. It converts true to 1 and false to 0, then displays the memory usage per element.

<html>
<body>
   <h2>Using the <i>Int8Array()</i> method to convert boolean values into 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;
      document.getElementById("div").appendChild(para);
      
      var typedArr = new Int8Array(array);
      var para2 = document.createElement('p');
      para2.innerHTML = "Array with binary representation: " + typedArr + "<br>Space taken by every element: " + typedArr.BYTES_PER_ELEMENT + " bytes";
      document.getElementById("div").appendChild(para2);
   </script>
</body>
</html>
Elements in an array: true,false,true,true,false,true
Array with binary representation: 1,0,1,1,0,1
Space taken by every element: 1 bytes

Using the ArrayBuffer Object

ArrayBuffer is a container for storing raw binary data. To access its content, we must use DataView objects or Typed Arrays. These objects provide methods to read and write data from ArrayBuffers.

Syntax

var buffer = new ArrayBuffer(16);
var view = new Int8Array(buffer);
view[0] = true; // or false

Example

In this example, we create an ArrayBuffer and use Int8Array to access it. We assign boolean values to array elements and display their binary representation.

<html>
<body>
   <h2>Using the <i>ArrayBuffer object</i> to convert boolean values into 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 = "Binary representation for true: " + binary[0].toString() + "<br>Binary representation for false: " + binary[1].toString();
      para.style.color = "red";
      document.getElementById("div").appendChild(para);
   </script>
</body>
</html>
Binary representation for true: 1
Binary representation for false: 0

Comparison of Methods

Method Use Case Memory Efficiency
Int8Array() Direct array conversion 1 byte per element
ArrayBuffer + Int8Array Manual buffer management 1 byte per element

Conclusion

While JavaScript doesn't provide a dedicated Boolean Typed Array, you can efficiently convert boolean values to binary data using Int8Array or ArrayBuffer with typed array views. This approach maintains memory efficiency while providing the binary representation needed for data processing.

Updated on: 2026-03-15T23:18:59+05:30

462 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements