Unlimited WordPress themes, graphics, videos & courses! Unlimited asset downloads! From $16.50/m
FREELessons:18Length:1.8 hours

Next lesson playing in 5 seconds

  • Overview
  • Transcript

2.5 Operators

In this lesson, you’ll learn about operators and the various types thereof. Let’s begin with “arithmetic” operators.

2.5 Operators

Hi, welcome to lesson number six where you'll learn about operators, and there are a few different types. Let's begin with the assignment operator, since this is the most basic. Now, the assignment operator will basically assign a value to its left operand based on the value of its right operand, and the simplest one is the equal operator. So let's say, for example that I have a variable x equals 1. We assign the value of one, the right operand, to x, the left operand. So now if I type x that's gonna be equal to one. I can also have a variable for example y equals 3. And then what I can do is say x equals y, and now x will have the value of three. So that's the basic form. Now there are also compound assignment operators. For example, the addition assignment. That looks something like this, x equals x plus y. And that gives us six but I can also say x plus equal y. x plus equal y is the same thing as x equals x plus y, similar to these are subtraction, multiplication, division and a bunch of others, but I'm just gonna show you a few examples here. So the subtraction will be x minus equal y, the multiplication is gonna be x multiply equal y and the division will be x slash equal y. And just like the addition assignment, you can already deduce their four forms, so, for example this subtraction is x equals x minus y. And that's a quick look at the assignment operator. Now let's move on to the comparison and logical operators. And the comparison operator will basically compare its operands and return a value based on the result of that comparison. So if the comparison is true it's gonna return true. If it's not, it's gonna return false. Now I'm sure you already know what the comparison operators are. It's equal to, it's greater than, smaller than, greater than or equal to, and so on and so forth. So you see where I'm going here. Let's see a few examples. So, if I say x is higher than y, it's gonna give me true, because currently x is six, y is three. What if I say x equal to y, it's gonna give me false, because those two values are not equal. I can also say x not equal to y, and that's gonna return true. And there's also a strict equal that I want to show you real quick. So if I say x strict equal so three equal sign y, it's gonna give me false. But if I set y to 6 and then I repeat x strict equal y it's gonna give me true. But what if I'm doing x equals 6, so instead of a number I'm assigning the string to the y variable, right? Then if I say x equals y it's gonna give me true because it's six and six, but if I say x is strict equal y it's gonna give me false and that's because strict equals also considers the operand type. If you do just is equal, so two equal signs, it's just gonna compare the value while strict equal is gonna look at the type as well. Now the logical operators are and, or, and not. So and, let's say for example x = true and y = false. If I say x and y, it's gonna give me false because the and operator will return true if both operands are true, otherwise it's gonna return false. If I do x, excuse me, x or y it's gonna give me true because the or logical operator will return true if either of the operands is true. If both are false, it's gonna return false. Finally, the logical not operator will basically switch things around. So x is currently true but if I say not x it's gonna return false. And y is currently false, so if I say not y it's gonna give me true. Basically the not operator is gonna return false if it's operand can be converted of true, otherwise it's going to return true. Now let's talk about the arithmetic operators and you've all ready seen a few like plus minus. These are arithmetic operators but there are a few that are a bit more special and I would like to show you those. So first of all we're gonna talk about the increment operator and this is a unary operator which means it only needs a one operand. It's not like for example the equal operator which needs two operands, right? So the increment operator basically will add one to its operand. So for example if we set x to three, and I say, x plus plus and I read x after that is gonna give me four. So, what happens is x plus plus will first print x and then it's gonna increment it by one. If I do plus plus x, it's gonna give me five because it's first gonna increment it, and then it's gonna print it. Same goes with decrement also an unary operation, instead of adding one to its operand it subtracts one. So if I say x minus minus, x is going to be equal to four and it also works the other way around minus minus x.. Now there is also a unary negation and unary plus, and the unary negation basically will return the negation of its operand. So if x is currently three, if I say minus x that's gonna give me minus three. The unary plus will actually attempt to convert its operand to a number, if of course it's not all ready. So, for example, plus 3 will give me 3. But if I have plus in the string of three it's gonna give me 3. It's gonna transform that type into a number. If I say for example plus true, it's gonna give me 1. Plus false is gonna give me 0 because zero and one are considered false and true. Finally we're gonna talk about the concatenation operator or the plus and that can be applied to a string. So for example if we're going to set some strings here for example x equals Hello and y is gonna equal to world, all right? What I can do is say x plus y and that's gonna give me Hello world. I can also do something like x plus equal and I can add another string to it. For example hello again and that's going to give me Hello hello. And I can also do this, 32 plus, 32 32 is gonna give me that big string. And that's a very quick look at operators in JavaScript. Now there is a programming concept that's I think is essential for any kind of JavaScript developer, and that is the if/else statements. Again if you've done computer science in school then you already know what that is. But we're gonna cover that in the next lesson. See you there.

Back to the top
View on GitHub