32

I am learning javascript and I want to initialize a boolean array in javascript.

I tried doing this:

 var anyBoxesChecked = [];
 var numeroPerguntas = 5;     
 for(int i=0;i<numeroPerguntas;i++)
 {
    anyBoxesChecked.push(false);
 }

But it doesn't work.

After googling I only found this way:

 public var terroristShooting : boolean[] = BooleanArrayTrue(10);
 function BooleanArrayTrue (size : int) : boolean[] {
     var boolArray = new boolean[size];
     for (var b in boolArray) b = true;
     return boolArray;
 }

But I find this a very difficult way just to initialize an array. Any one knows another way to do that?

7 Answers 7

102

I know it's late but i found this efficient method to initialize array with Boolean values

    var numeroPerguntas = 5;     
    var anyBoxesChecked = new Array(numeroPerguntas).fill(false);
    console.log(anyBoxesChecked);

Sign up to request clarification or add additional context in comments.

3 Comments

Great answer. To read more about the method refer developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
Just a side note, this method is not supported on IE
Is that 1D or 2D?
14

You were getting an error with that code that debugging would have caught. int isn't a JS keyword. Use var and your code works perfectly.

var anyBoxesChecked = [];
var numeroPerguntas = 5;     
for (var i = 0; i < numeroPerguntas; i++) {
  anyBoxesChecked.push(false);
}

DEMO

4 Comments

Don't forget to tip the waitress on your way out :)
@AlbertoCrespo, by the way that second code example you gave was UnityScript rather than JavaScript. They're similar in some ways but that code would never have worked outside of a Unity environment.
Thank you, i didn't know the existence of that!
amazing answer very useful
4

You can use Array.from Array.from({length: 5}, i => i = false);

1 Comment

I think this could be improved by using the map function (second argument) correctly. To expand, using i there implies the first argument is the index, when it's in fact the element at that index (see docs you linked). Also, assigning to i isn't what's making this work; it's the fact that i = rvalue returns rvalue, which is all you have to do for this to work. Hence, Array.from({length: 5}, _ => false); IMO would be a better answer.
2

it can also be one solution

var boolArray = [true,false];
console.log(boolArray);

Comments

0

You may use Array.apply and map...

var numeroPerguntas = 5;  
var a = Array.apply(null, new Array(numeroPerguntas)).map(function(){return false});
alert(a);

Comments

0

Wouldn't it be more efficient to use an integer and bitwise operations?

<script>
var boolarray = 0;

function comparray(indexArr) {
  if (boolarray&(2**(indexArr))) {
    boolarray -= 2**(indexArr);
    //do stuff if true
  } else {
    boolarray += 2**(indexArr);
    //do stuff if false
  }
}
</script>

Comments

0
    Array.from({length: 5}, () => false)

Sample:

const arr = Array.from({length: 5}, () => false)
console.log(arr.join(", "))

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.