If you already have a subscription, you can sign in.
Enjoy free content straight from your inbox 💌
00:00
Perhaps the similar stenotype in all of JavaScript is the humble bullion, but nonetheless, it is critical to writing useful programs. So let's take a look. A bullion is a general programming concept and it always has two values and within JavaScript, these values are represented by the literals, lowercase two and lowercase. False. Using the JavaScript type of operator on a bullion type gives back the string bullion, which is of course why we are calling them bullion. We don't normally use a bullion by itself as it often shows up as a result of a comparison operator. For example, hey, we have a variable called person,
00:34
which we have initialized to John, and if we apply the triple equals operator, which is called a strict equality within JavaScript between the person and the string, John, we get back through. Similarly, if we compare it to something else, for example, the string chain, we get back the Boolean false. The key use case for a Boolean is when it shows up as a result in conditional logic. For example, hey we have a person that might be Judy or it might be Kate. If person is going to be Judy, we are going to log to the console Hi J, and if it's going to be Kate, we are going to log, Hey Kate,
01:08
right now we have initialized person to Judy. This means that the triple equal comparison with the string Judy will be true. And similarly, the triple equal comparison with gait will result in false. The way the if condition works within JavaScript is if the expression provided to if is toothy and of course true is a toothy value, then the block of code contained within if will execute. And similarly, if the expression evaluates to a falsey value and false is a falsey value, then the block of code will be skipped over and if we execute this code, no surprise we see Hi J.
01:41
Now if you were to modify the person variable to be Kate and then execute the code again, of course this time we see hey K in terms of bullion operators, one that we really need to care about is the exclamation point which is lovingly called the bank and when used twice it is called the double bank. The exclamation point is the negation operator and it is also called the logical knot. And the logical compliment its behavior is quite simple. It takes it true and turns it into a false and it takes a false and turns it into a true and you can actually use it twice and for a bullion it would have no side effect. So the two will become false and then will become true again.
02:19
And similarly false will become true and then false back again. But this is great for converting falsely or truthy values of other data types into true bullion. True and false and empty string within JavaScript is a falsey. So applying double bang to it converts it into a true Boolean false. And similarly, a non empty string is actually truthy, so applying double bang to that converts it to a Boolean. True using truthy and Falsey values within conditions is normally frowned upon and you really want to use real bullion, true and false and using the double bang allows you to convert these into real
02:53
bullions. We can combine to bullion expressions with the operators double and percent For and and double pipe for. Or here we have a variable defining a state that can be pending or done and because of some unforeseen circumstances it might end up being something else, in which case we want to show an error. So if it is indeed pending, we show valid. If it is done, we show valid. Otherwise we show unexpected state notice a duplication of code between the pending and the done code blocks. Wouldn't it be great if you could combine these both conditions into a single
03:28
conditional and that is where the OR operator comes into play. If either of these conditions state equal to pending or state equal to done are true, then the whole of the condition will turn into true. That is how the boo or operator works and it allows us to combine those two conditions into a single conditional block so we don't have to repeat ourselves again and again. The opposite of the boo or is the BOO and operator. And unlike or where either of the conditions being true results in the whole thing be true with the Boolean and all of the conditions have to be true for the final expression to result to true.
04:02
Here we have a variable for an S T T P status code, which can be between one hundred and five nine nine. If it is 200, it is success, but if it is in the range 400 to 500, we want to show an error and to combine the conditions that it must be greater than equal to 400 and less than equal to 500. We use the Boolean end. So as an example, right now it is 200, so it'll fall down to success, but if it is four, four not found, it is going to lie in the range 400 and 500 and therefore we will log out error and for any other value. For example, 3 0 1 moved permanently, it is not 200,
04:39
it is not in the range 400 to 500 and therefore we log out unexpected status when you have a variable that is intended to store a bullion value, some developers have a very strong feelings about what these variables should be named and specifically how they should be prefixed. Here are a few examples to give you an intuition about the most common prefixes. If you want a variable that will be true if the user is logged in. Don't call it user logged in because it's confusing. If this is a bullion value or an object containing some details about the user that is logged in instead,
05:12
prefer the more obviously Boolean looking is user logged in. Similarly, if you have a bullion for when the user has access, don't call it access because it looks more like an object and instead prefer has access. If you want a variable to be true, if the user can dance, then prefer can dance. Similarly, if you want something to be true, if the user should provide an address, use should provide address. Notice how the prefixes are naturally appearing in the comments as well. And in terms of the English language, they fall under categories of verbs, but personally I recommend picking it up from whatever sentence you would
05:47
naturally use to describe these variables. One thing that even I strongly feel about is when variables have negation in the name as an example, do not Name a variable is not allowed. Don't use any negation term like the word not within a variable name. It's going to be perfectly fine and easy to understand when the value is true. For example, something is not allowed. However, when it's going to be false, you are going to need to do a double negative mapping in your brain is not allowed, is false, and therefore something is allowed. Things would be much better if we called.
06:21
A variable is allowed and it's very clear when something is not allowed and when something is allowed. And these are the same as the previous conditions, which were a bit hard to understand. Now that we've taken a deep look at bullions in isolation, it allows us to safely look at more attributes of the core JavaScript types. As always, thank you for joining me and I will see you in the next one.