Skip to content
DeveloperMemos

Getting a Random Boolean in JavaScript

JavaScript, Random Boolean1 min read

When working with JavaScript, there might be scenarios where you need to generate a random boolean value. Whether it's for testing purposes, decision-making logic, or any other use case, having a way to obtain a random boolean can be quite handy. In this article, we will explore different methods to achieve this in JavaScript.

Method 1: Math.random()

One of the simplest ways to get a random boolean value is by utilizing the Math.random() function. This function returns a random floating-point number between 0 and 1. We can utilize this behavior to generate a random boolean by comparing the generated value with a threshold.

1function getRandomBoolean() {
2 return Math.random() < 0.5;
3}
4
5const randomBoolean = getRandomBoolean();
6console.log(randomBoolean); // Output: true or false

In the above example, we compare the generated random number with 0.5. If the generated number is less than 0.5, the function returns true; otherwise, it returns false. This method gives an equal probability for both true and false outcomes.

Method 2: Boolean Conversion

Another approach to getting a random boolean is by converting a random number into a boolean value. In JavaScript, there are multiple ways to convert values to boolean using type coercion. One such method is to utilize the Boolean constructor function.

1function getRandomBoolean() {
2 return Boolean(Math.round(Math.random()));
3}
4
5const randomBoolean = getRandomBoolean();
6console.log(randomBoolean); // Output: true or false

In this method, we generate a random number using Math.random() and then round it using Math.round(). The resulting number is then passed to the Boolean() constructor, which converts it to a boolean value. If the number is 0, it is converted to false, and if the number is 1, it is converted to true.

Method 3: Random Index Selection

If you have an array with two elements representing true and false, respectively, you can select a random index from the array to get a random boolean value.

1function getRandomBoolean() {
2 const booleanOptions = [true, false];
3 const randomIndex = Math.floor(Math.random() * booleanOptions.length);
4 return booleanOptions[randomIndex];
5}
6
7const randomBoolean = getRandomBoolean();
8console.log(randomBoolean); // Output: true or false

In this method, we create an array booleanOptions with the two possible boolean values. We generate a random index using Math.random() and Math.floor(), and then use that index to retrieve a random boolean value from the array.

Wrap Up

Generating a random boolean value in JavaScript can be achieved using different methods. Whether you prefer using Math.random(), boolean conversion, or random index selection, each approach offers a straightforward way to obtain a random boolean value. Choose the method that suits your specific use case and enjoy the flexibility of generating random booleans in JavaScript.