For the most part if I want to set a boolean value I just set it using a boolean literal. When it comes to creating a boolean value by way of a literal then the true and false boolean literal keywords can be used do do just this. In this example I am setting the value of a logging variable to a boolean value of true that will be using in a logging function that will only log a given message of the logging variable is true.
For this example I have a boolean called firstRun that is set to true for starters, I then also have a loop that will fire once every second by way of using setTimeout to delay the next call of the method. The first time that the loop fires, a ‘first run’ message will log to the console, and the firstRun boolean will set back to false. Because I am using the firstRun boolean in an if statement, the ‘first run’ message will only fire once.
This might not be the best way to go about doing something like this as a better approach would be to have a completely separate method for doing anything that needs to happened during the first run of something. However that is a matter for another post, the goal here was to just demonstrate a use case for a boolean.
So literals are one way to end up with a boolean value, that is by just simply setting it to a variable with one of these true or false keywords. There are however several other ways to end up with a boolean value, for example they are often the result of expressions, and function calls. Also it might help to just look over a few more examples of booleans anyway so lets continue with this.
Boolean values can also be the result of an expression, that is a collection of numbers, strings, variables that contains such values combined with one or more operators. I will not be getting into operators and expressions in general here, that is a matter for another post. However I will be going over a few examples of expressions that evaluate to a boolean value in this section.
For example say I have a x variable that holds a number value and I want another boolean that will be true when the x variable is in a certain range, otherwise the value will be false.
Many projects that aim to make the source code as compact as possible take advantage of all kinds of tricks to reduce file size. There is using a library that does a decent job of reducing the file size by removing all whitespace, and preforming replace operations for all variable names to smaller variable names that are just one character. However there is then going beyond that and trying to find yet even more ways to crunch things down even more.
Sometimes I see the use of the expression !0 to replace the boolean literal true, and !1 to replace false as one way t go about crunching down file size even more.
This works because the number 0 evaluates to false, and the ! operator both converts to boolean and negates the value as well. For projects where I really do want to crunch down file size it might be called for, but it reduces readability for some developers also. You would think that doing this would not make a big difference, and in many cases you might very well be right about that. However if the volume of source code is large enough, little tricks like this could add up a bit. In any case there is still just understanding out very simple expressions such as this work.
So the ! operator converts a non boolean value to a boolean, but it is also negated. So just calling the operator twice will then negate it back to its original value.
This is yet another tick I see used often as a way to convert something to a boolean value, and then convert that boolean value back to the original boolean value that it will evaluate to.
Never the less when it comes to writing a post on boolean variables I suppose it is called for to cover this topic, as it is something that might pop up now and then here and there when reading various code examples.
When using the Boolean Object as a constructor it returns an object, and not a boolean. As such because the value returned is an object rather than a boolean value this might result in some problems when it comes to creating a boolean this way. When it comes to having one of these boolean objects the value of method of the object can be called to get the actually boolean primitive value of the boolean object that is returned.
Making booleans this way is not such a great idea. It makes doing so far more complicated than it needs to be, and can lead to unexpected results if you are not aware of the fact that an object evaluates to true.
When omitting the new keyword a boolean primitive value will be returned rather than an object, making it a way to convert to a boolean. This use of the Boolean method is a little more practical when it comes to converting values to a boolean type. However there are also a number of other ways to go about converting to boolean when it comes to working out expressions as a way to do so..
So then I can not say that I use the Boolean method this way that often also because the !! operator works just fine to get such a task done. Still the Boolean Object is something to be aware of as it is often used in examples.
There is also the simple fact that booleans to have there limitation after all it is a type where there are only two possible values. So if there are ways that I can just use numbers in place of booleans, this does not just allow for the same functionality by just using the numbers 0 and 1 for false and true, it also allows for additional values of course. So not only does the use of numbers allow for the same functionally, it also allows for additional possibles while we are at it.
One simple numbers for booleans replacement example might be the while loop trick with an index value that starts at a non zero value, but will reach zero at some point. For example I can set the index value for an element in an array to the length of the array, and then subtract from i in the while loop area that is used to evaluate if the loop should keep looping or not. This way as long as the index value is above zero the loop will continue, but it will reach zero at some point, and when it does that will evaluate to false, and the loop will stop.