Back to 30 Seconds Of Code

Generate a random boolean value in JavaScript

content/snippets/js/s/random-boolean.md

14.0.0528 B
Original Source

JavaScript doesn't have a built-in function to generate a random boolean value, but you can easily create one using Math.random(). All you need to do is check if the result of Math.random() is greater than or equal to 0.5.

As Math.random() generates a random number between 0 and 1, the probability of the result being greater than or equal to 0.5 is 50%, which makes for an even distribution of true and false values.

js
const randomBoolean = () => Math.random() >= 0.5;

randomBoolean(); // true