Open In App

How to Generate a Random Boolean using JavaScript?

Last Updated : 09 Oct, 2024
Comments
Improve
Suggest changes
1 Like
Like
Report

To generate a random boolean in JavaScript, use Math.random(), which returns a random number between 0 and 1.

Approach: Using Math.random() function

Example 1: This example implements the above approach.


Output
false

Example 2:

  • Create an array containing 'true' and 'false' values.
  • Calculate Math.random() and round its value.
  • Use rounded value as the index to the array, to get boolean.

Output
true

Next Article
Article Tags :

Similar Reads