3

If I do:

var arr=Uint8Array(1);
arr[0]="hi";
console.log(arr[0]);

The result is 0 instead of "hi". That’s because that type of object (Uint8Array) only admits 8-bit unsigned integers as values for its items.

Is there some type of array-like object made to store only boolean (true/false) values?

How can I create an object of that type?

Note: Please, don’t use watch; it’s not cross-browser. I’m going to use it in any browser.

Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75
t33st33r
  • 197
  • 2
  • 9
  • 1
    `var arr = [true, false, true];` doesn't work for you? – Oleg Apr 29 '15 at 20:08
  • 1
    var arr=[true,false,true]; arr[0]="test"; console.log(arr); Try that. – t33st33r Apr 29 '15 at 20:10
  • 1
    It returns `["test", false, true]`. What's the problem? – Oleg Apr 29 '15 at 20:12
  • 1
    **"test"** is not a boolean value. The idea is to make an array able to store boolean values exclusively. – t33st33r Apr 29 '15 at 20:14
  • The problem here is, you are assigning a string value to an Unit8Array. You can use it for only integer values. A Unit8Array can be used to store values from 0 to 255 I guess – The Guest Apr 29 '15 at 20:19
  • 2
    Have you looked at http://stackoverflow.com/a/6973312/123415? – jcuenod Apr 29 '15 at 20:21
  • 1
    Honestly, why down vote this question? – jcuenod Apr 29 '15 at 22:19
  • [Here](https://stackoverflow.com/a/76619183/213871) I made it in TypeScript, but you can use the `Proxy` class in pure javascript to simulate a boolean typed array with an API surface that is similar to other typed arrays. – ceztko Jul 05 '23 at 10:34

5 Answers5

1

This isn't exactly what i asked for, but it could be a good implementation:

Uint8Array.prototype.getBitSum=function() {
 var _t=0;
 [].forEach.call(this, function(v, i) { _t+=Math.pow(256,i)*v; });
 return _t;
}

Uint8Array.prototype.getBit=function(b) {
 var _t=this.getBitSum();
 return ((_t & Math.pow(2,b)) != false);
}

Uint8Array.prototype.setBit=function(b, v) {
 var _v=v;
 if (isNaN(_v)) return false;
 var _B=Math.floor(b/8), _B_b=(b%8);
 if (_v) this[_B]|=Math.pow(2,_B_b);
 else this[_B]&=(255-Math.pow(2,_B_b));
 return true;
}

My limited knowledge about some terms made of this a hard work for me. If you find some bug... add comment, please.

The reason of this is to minimize memory reservation. So I think it does right.

t33st33r
  • 197
  • 2
  • 9
  • 2
    You could look up the << and >> operators. – Sophiα2329 Oct 30 '15 at 17:26
  • You are practically doing bit packing which is an ideal way of creating a boolean typed array in JS. The only thing you may change is the `Math` object methods to their bitwise equivalents which are expected to result faster. Like dividing by `Math.floor(b/8)` is like `b >> 3` whereas modulo `b%8` would be just `b&7` etc... Check [this nice article](https://jacksondunstan.com/articles/1946) – Redu Apr 21 '19 at 11:26
0

You cannot have boolean-only arrays natively in JavaScript. Especially with legacy browsers support. Check the type of a value before inserting it into your array. If it's not a boolean, i.e. typeof x === 'boolean' then don't insert it.

Oleg
  • 9,341
  • 2
  • 43
  • 58
0

The only types supported for JavaScript typed arrays are the following:

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

For anything else, you'd have to use standard JavaScript arrays like this:

var a = [true];
a[0] = false;
console.log(a); // false

Note that JavaScript being a dynamically typed language will let the developer also put values of other types into the array (such as "hi"). You can enforce types using functions that modify the array for the user after double checking the type:

function set(index, value) {
    if (typeof value != 'boolean') {
        throw new Error('Expected a boolean');
    }
    a[index] = value;
}
Blixt
  • 49,547
  • 13
  • 120
  • 153
0

No, there's not. Here's the MDN documentation on typed arrays in JS and you can see there's only typed arrays for various sizes of integers and floats.

As for why there's no "boolean typed arrays"? Well, I think the primary purpose of typed arrays in JS is for efficiently handling large amounts of data for APIs, and there's really not a ton of demand for APIs to take a large amount of boolean data.

In short, the primary purpose is performance, not type-safety. The purpose of typed arrays isn't really to prevent the wrong type of data from being added to the array.

But, if you really want a "boolean array", you could write a wrapper around a UInt8Array that stores and reads booleans, using bitwise operations to store the booleans as individual bits. (i.e. something like this answer, though backed by a typed array) But I'd question the value in doing so.

Community
  • 1
  • 1
Retsam
  • 30,909
  • 11
  • 68
  • 90
0

You are asking for a bitset in JavaScript. There are libraries implementing bitsets in JavaScript, see for example :

https://github.com/lemire/FastBitSet.js

Daniel Lemire
  • 3,470
  • 2
  • 25
  • 23