JS – Randomly generate a Boolean value

Boolean values and logic, named after George Bool, the creator of such, work rather simply. If an expression results in a true statement, perform a certain series of events; otherwise, perform a different series of events. Boolean logic is such a key part of programming that practically every programming language, even ones you might not consider to be programming languages such as MySQL, has Boolean logic implemented and available for you to use. However, Boolean data types are not standard across implementations. Some use true and false, along with the variation True and False, while others use 0 and 1 for false and true, respectively (this actually corresponds with the binary number system computers use).

As already explained, Boolean logic and data types make code much simpler. Yet they are not only used for decision-making. They can also be used to represent a value. Say you are writing a program that checks if how many laptops in your store inventory have built-in number (num) pads. You can use a Boolean value to flag each laptop item as having a numpad, true, or not, false. Then in your code, you simply check that valve and perform the appropriate action.

Now what if you wanted to write some code that generated hypothetical laptops, and one of the laptop properties was if it had a numpad or not. You would probably create laptop objects containing the laptop’s features, or properties.

GitHub Gist link

Remember, since all properties are randomly generated, how can you randomly generate a Boolean value for the numpad and other properties of that nature? You know how to generate random numbers and select items from an array, but what about Boolean values? Those are tricky!

There are two ways you can do this. One is more familiar, but the other is a one-liner action. In this tutorial, we will explore both methods. Remember, if you need an easy way to try out this code, a JavaScript interpreter is available in the Codecademy Labs or your browser’s console, and detailed explanations of all the functions methods used here are on MDN. 🙂

  1. The first option you can use is to create an array containing true and false as values and use your knowledge of selecting items from an array to decide if the laptop will be blessed with a numpad (I hope so!).

    GitHub Gist link

    This is a perfectly valid and working option, but as you can see it adds a few extra lines to your script. The same thing can also be accomplished as a one-liner (unfortunately the explanation cannot be that short), as you are about to see.

  2. Remember how I said some programming languages use 0 and 1 to represent false and true, respectively? We can use this to our advantage. JavaScript is cool by treating a zero as false and any non-zero number as true. That means we can generate a random number between zero and one and use that as our Boolean value!

    I know, I know, you are probably thinking to yourself right now:

    Yes, that is pretty cool, but how do I convert it into a Boolean value? A zero or one could make the property’s value more confusing later on or for someone who did not know JavaScript acts this way. A Boolean value would clear that potential confusion right up.

    I fully agree with your concern. That is why there is one more trick to this. With our number, we can use a double negation, !!, or the Boolean() constructor to convert our number to a true or false value.

    // Double negation
    !!Math.floor(Math.random() * 2);
    // Boolean() constructor
    Boolean(Math.floor(Math.random() * 2));

    Which one you use is mostly personal preference. There are strong opinions about each method just on this Stack Overflow topic alone, and I am sure there are other strongly-written questions and articles on the matter as well.

    Armed with this new-found knowledge, let’s update our example to use it. For brevity, I will use the double negation method. 😉

    GitHub Gist link

    To reiterate, this will do the exact same thing as the code in option #1, but it does it in a shorter, more concise way. Which one you use is ultimately up to you.

Congratulations! You now know how to randomly generate a Boolean value! 😀 Now go out there and not make up false truths! 😛