Hi again! Now, last I spoke (er… wrote) I had been going ad nauseum regarding the affects of unary and negation operators on strings. Today, I’ll be focusing my efforts with regards to these same operators but on a few of the remaining objects on the list: booleans and numbers… The others I’ll handle in future posts.
+5 == 5
-5 == -5
-(-5) == 5
Well… Surprise, surprise… The plus and minus do the exact expected thing one would expect in standard mathematics…
The only exception to this is --5. --5 actually won’t evaluate as the -- operator is reserved for pre-decrement and post-decrement of variables, and not numbers… For the negation of a negative number parentheses need to be used, as per my last example.
And that’s UNOs with numbers… I told you it’d be quick…
Now we get to the interesting parts. Let’s start with booleans…
+false == 0
+true == 1
- Truthy values will coerce to true when converted to a Boolean. Examples of truthy values are all natural numbers both positive and negative (excluding zero), and non-empty strings, and all empty and non-empty arrays and objects. Basically any value that has definition, is not null, or zero, or empty qualifies as a truthy value (…and empty objects and arrays, I wish someone can explain this to me… That’s just plain weird). True as well counts as a truthy value.
- Falsy values will coerce to false when converted to a Boolean. Examples of falsy values are zero, empty strings; null, NaN (Not a Number) and undefined. Naturally, false also counts as a falsy value.
So where am I leading with this? If you’ve been programming for a while, you’ll have come across similar examples of truthy and falsy values at work, probably without you realizing it:
The while loop and the for loop are notorious for abusing the concepts of truthy and falsy values. See that solitary x in our example here? As you will know while loops work off a condition being satisfied to continue running. When the condition is no longer met, the while loop terminates and the code moves on. With each iteration the value of x decreases until it reaches zero. Once it does, the while loop finally converts zero to false and terminates. Similarly, you may have seen the following:
Both statements mean the same thing… So, it would make sense that true and 1 are considered equivalent to one another logically. In fact, if you were to evaluate !!1 you would naturally get true, and evaluating +true will give you 1. Similarly, because zero is a falsy value you could conclude that !!0 would give you false, and therefore +false would give you 0.
And you’d be right…
Because 0 and 1 are falsy and truthy respectively they can technically be used as shortcuts for false and true respectively. While technically any number or truthy value could be used as a substitute for true, 1 is the simplest and quickest way. This little trick of using 0 and 1 as substitutes for false and true is often used in code golf (which I will cover in a later post) to conserve character usage. Basically, you’re saving 4 and 3 characters respectively.
So as a result:
+false == 0
+true == 1
In my next post, I will be addressing arrays and objects and how they react with UNOs. Stay tuned.