Transcript from the "Operations" Lesson

[00:00:00]

>> Kyle Simpson: So what are some of these operations that we might want to do with our values?

>> Kyle Simpson: Well, you notice here, on line one, I've got a 3, and then a plus and then a 4. So I've got two numbers that I wanna add together. Or I've got the number 43, and a minus and a 1, and I wanna subtract one number from the other, okay?

[00:00:21] So those are basic arithmetic, if you will. You probably learned this back in kindergarten, first grade. Basic arithmetic that you can express in your programs. And you don't often write them out literally, like 3 plus 4. Often, those values will be inside of variables. And we'll talk about that in just a moment.

[00:00:38] But it is possible for you to just do straight up math, or any other sort of operation. For example, line four, you'll notice that I also have a plus operator. But I don't have numbers here, I have strings. And one of the peculiarities of JavaScript is that it uses the plus operator to mean two different things depending on what kind of values we've got in the operation.

[00:01:01] And so, when we have string involved in the plus, we do what's called a string concatenation, which is like just smooching two strings together, that's all it means. And when there's two numbers involved, that's math. So that plus operator, there's a fancy term for this in programming, it's called overloaded.

[00:01:19] It means that it has more than one kind of behavior, depending upon the values that are involved in. By the way, I keep talking about involve in. What do I mean here? Well, operators have what are called operands. And an operand is the value that is going to be operated on by the operator.

[00:01:38] So on line one, 3 plus 4, the operator is plus, it's an addition, a numeric addition. And the two operands are 3 and 4. Oftentimes, we might refer to them as the left operand, which would be 3, and then the right operand, which would be 4. Those kinds of operators, ones that have a left and a right in terms of their operands, those kinds of operators are called binary operators.

[00:02:05] And binary is just a fancy term for it means there's two involved. So we have binary operators, like plus and minus. But you notice on line six, I've got a special character, the exclamation mark. That's a different kind of operator. That's what we call a unary operator. Unary means one, kind of like in Spanish uno means one, you can think unary means one.

[00:02:32] So unary means a single operand involved in the operation. And here, we have the false, the exclamation mark does what we call flipping of a Boolean value, or the negation or the compliment of a Boolean value. So remember how I said Booleans can only be false or true.

[00:02:50] So if we start out with false and we wanna flip it to the other one, we're gonna get true. And if we were to start out with the exclamation mark and true and flip it, we'd get false, right? Now, line eight shows a different kind of operator. Still, that is a binary operator, but it's not actually producing sort of a different kind of value.

[00:03:10] Here, it's doing what we call comparison. It's saying does the value on the left-hand side and the value on the right- hand side, do they have some sort of relationship that we can determine something about? In this case, we're using the double equals operator, which is the equals or what we call the Lucy quality operator.

[00:03:29] It's saying, are these two values equal to each other?

>> Kyle Simpson: By the way, you may have heard inequality, and we'll get into this in a little while. You may have heard inequality, in JavaScript that there's two different equality comparison operators. There's this double equals one, and then there's the triple equals one.

[00:03:47] And there's a lot of nuanced distinction between the two. I'm gonna try to help you juggle some of that later in our discussion in this course. But for right now, you should know that in this particular case, they wouldn't have done anything differently. We would have gotten the exact same result between a double equals and a triple equals because both of them are numbers, so no changing of value types is even involved.

[00:04:11] Thy would have done exactly the same thing. So 3.0 and 3, if you remember when I talked about values a little while ago, 3.0 and 3 are the same kind of value. They're both numbers, so they're equal to each other. Even though in our programming code, they look slightly different, they're actually basically the same number, so that comparison would tell us that they are equal.

[00:04:31] It would return us actually a Boolean that would say true. Line 10 is another kind of comparison, which is less than. And we know that the number 3 is clearly less than 4. So again, when we do that kind of comparison, we would've gotten a Boolean true back as well.

[00:04:47] Now, line 12 shows us yet still a different kind of operator. And these are sort of conditional operators that are, I'm sorry, not conditional operators, they're logical operators. They basically tried to decide something for us. So the double vertical line there, that's called the or operator. The single character is called the pipe character on your keyboard, if you haven't seen that before.

[00:05:11] When we put two of them together, that's called the or operator. And so, it's basically saying tell me if one or the other of these results in something that would be we would call Boolean true. If one or the other results in true, then I wanna know about that.

[00:05:28] Otherwise, we won't do something. And when we talk about making decisions in a little while, you'll get the sense of why you might wanna do something if one or the other of these things is true, do it, and if not, do something different. So the double pipe operator is what we call or.

[00:05:44] And there's a complement to that one called the double ampersand. That little squirrelly character that nobody can ever write. That little double ampersand operator is how we say and. So that would give us a true only if both of the things, both of the operands, were true. So now, you're starting to get a sense that there are things that we wanna do with our values.

[00:06:08] And the things that we wanna do are the kind of operations that we're gonna perform. Before we move on, I wanna actually let you see those expressions, those operations that we were just talking about, actually being evaluated. So you can see here that 3 plus 4, it evaluated to 7.

[00:06:23] And 43 minus 1 it evaluated to 42. And when I concatenated these two strings together, you see the string Kyle Simpson. The negation of false, or the compliment of false, it evaluates to true. And 3.0 equals 3. And by the way, even if I did a triple equals, it's still true.

[00:06:42] Because it's not about type coercion, it's just that JavaScript treats these numbers as the same. We can see that 3 less than 4 is evaluating to true. But if I were to flip that around and say 4 less than 3, now, all of a sudden that one's gonna change to evaluating to false.

[00:06:58] And finally, true or false, of course, true or false is gonna evaluate the true. But if I changed it to the double ampersand, now, of the sudden, that one was also false. So definitely, make sure you're familiar with how these things are evaluating. It's not always so great to do it in your head, sometimes it's great to just run it in the JavaScript and see what you get.