For the most part if I want to set a boolean value I just set it using a literal. The true and false boolean literals can be used do do just this. For this example I have a boolean called firstRun that is set to true, I then also have a loop that will fire once every second by way of using setTimeout. The first time that the loop fires, a ‘first run’ message will logg, and the firstRun bool will set back to false.
Boolean values can also be the result of an expression. 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.
many projects that aim to make the source code as compact as possible take advantage of all kinds of tricks to reduce file size. Sometimes I see the use of the expression !0 to replace the boolean literal true.
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 as well.
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.
When using the Boolean Object as a constructor it returns an object, and not a boolen. However the valueOf method can be used to get the boolean value of the Object.
Making booleans this way is not such a great idea. It makes doing so far more complicated than it needs to be, and can lean to unexspected 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.
I do not use this as well, 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.