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
false, along with the variation
False, while others use
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.
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!
functions methods used here are on MDN. 🙂
- The first option you can use is to create an array containing
falseas 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!).
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.
- Remember how I said some programming languages use
falseand 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:
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
// 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. 😉
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