So today, we’re talking Booleans. Booleans are, as you’re probably aware, one of two states: **true** or **false**. This is a beautiful thing… True or false, black or white, left or right, right or wrong, shaken or stirred, scrunch or fold… Sorry… Let’s digress…

Now, with Booleans you have the **Logical NOT Operator**, represented by our friend, the exclamation point, (the **!**) What the Logical NOT Operator does is take your object and go all truthy-falsy with it. Basically, the Logical NOT Operator is tasked with finding *the logical opposite of whatever your object represents*.

So based on the rules I mentioned about what is a truthy value and what is a falsy value we get a series of rules to live by. So let’s take a quick look at Boolean types.

- If
**x**is**true**, then**!x==false** - If
**x**is**false**, then**!x==true**

This is pretty much the essence of it all… The Logical NOT Operator here acts as a “The Opposite Of…” operator. Opposite of true is false and vice versa… Let’s now push this further to numbers:

- If
**x**is zero, then**!x==true** - If
**x**is non-zero, then**!x==false**

Okay, so zero, the *only* falsy value in the JavaScript natural number system is therefore the only number that will yield **true** when coerced by the Logical NOT Operator. Subsequently, all other numbers being truthy when coerced by a Logical NOT Operator will yield **false**. You may recall in one of my previous posts that** !0** and** !1** have occasionally been used as alternatives to **true** and **false** respectively. I will probably go a step further and say that if you were happy to push the boundaries of your code if minimizing or code-golfing, you could technically set specific Booleans to **1** and **0** to represent **true** and **false** respectively. Bear in mind the following:

While

0==falseand1==true,

!1===falseand!0===true

If you’re scratching your heads about the differences between **=**, **==** and **===**, let me illustrate it with a very simple set of rules that makes it easy to remember:

- If you are assigning a variable, you only need the one
**=** - If you need to compare values only, you need one more to get
**==** - If you want to compare values and object types, you need one more on top of that to get
**===**

So, let’s use these rules in a practical example which you can perform in the JavaScript console.

**x = “5”; **// We’ve set **x** to a string, typically numeric 5 as a string, NOT the actual number.

**x == 5; **// You’ll get **true
**

This is because **x** is compared against the numeric version of the string, forcing the string **“5”** on the left hand side to be coerced into a numeric **5** to compare against the **5 on the right-hand side**. As you’re comparing values, the now-numeric 5 on the left is compared to the existing numeric 5 on the right; 5 is equal to 5… As a result, you’ve got a **true** statement. But if we try this with **===** we get something quite different…

**x === 5; **// You’ll get **false**

Why do we get **false** this time around? In this case we’re now adding another level of comparison, we’re comparing against not only value, but object type… We’re comparing a string against a number. As a string is not a number (or vice versa), the **===** comparison operator fails right there and then, returning **false**.

So, bear that in mind if you’re thinking of taking some shortcuts with **true **or **false**.

So where were we? We’ve looked at Booleans, we’ve looked at numbers… Let’s take a look at strings and the effects of Logical NOT Operators on these:

- If
**x**is an empty string**“”**, then**!x==true** - If
**x**is non-empty string like**“margarita”**, then**!x==false**

Empty strings are falsy values, and so a Logical NOT Operator on a falsy value will yield **true**. Consequently, a non-empty string, which is a truthy value when coerced by a Logical NOT Operator will naturally yield **false**.

Now it’s time for arrays:

- If
**x**is an empty array**[]**, then**!x==false** - If
**x**is non-empty array like**[“margarita”]**, then**!x==false**

Herein lies the rub; we may be dealing with an empty array, but **it is still a defined array**. As a result, it still has value, and is thus truthy, thus **![]** will yield **false**.

Bear in mind that an array with a falsy value as its only element (such as **[“”]** or **[0]**) is still a non-empty array, and like all non-empty arrays, will yield **false** when coerced by a Logical NOT Operator.

Now, back to objects… The truthy little buggers that they are. An object is truthy regardless of whether it is empty or non-empty. Case in point:

- If
**x**is an empty object**{}**, then**!x==false** - If
**x**is non-empty array like**[“pizza”:”supreme”]**, then**!x==false**

So, put simply, JavaScript object types don’t really respond well to change when it comes to coercing them to a Boolean with a Logical NOT Operator…

So, what do we do about the other objects – the dates, the functions, the regular expressions? We can test those out too… So, let’s take a quick look at Dates

**!(new Date) **// returns** false**

Why has it returned **false**? Date objects are truthy. And thus a Logical NOT Operator coercing a truthy value will result in **false**.

How about regular expressions? They’re truthy as well… Put a Logical NOT Operator in front of a truthy value and you will get **false**:

**!/tic|tac|toe/** // returns **false**

Lastly, functions… Functions when coerced by a Logical NOT Operator act similarly to how functions behave when coerced by a unary or negation operator, the end result is basically what you get after the operator has acted on the return value of the function. If a function’s return value is truthy, the Logical Not Operator acting upon the function will result in **false**; whereas if a function’s return value is falsy, the Logical Not Operator acting upon the function will result in **true**.

Case in point: **x = function(y) { return y + 1;}**

**!x == false**is defined, therefore truthy, thus

x**!x**is**false**.**!x(-1) == true**

**x(-1) == 0**, therefore falsy, thus**!x(-1)**is**true**.**!x(0) == false**

**x(0) == 1**, therefore truthy, thus**!x(0)**is**false**.

Let’s try a function with *no* return value: **y = function(z) { var x = 17;}**

**!y == false**is still defined, therefore truthy, thus

y**!y**is**false**.**!y(27) == true**There is no return value for the function, as

**y(27)**returns**undefined**.**Undefined**is a falsy value, as a result,**!y(27)**is**true**.

So there’s the Logical NOT Operator and how Booleans can be coerced from other objects using it. The final operator of focus is the concatenation operator, which funnily enough is also represented by the **+ **character. I’ll address the concatenation operator in my next post. Stay tuned!