Operators are one of the basic ways that you’ll tell a computer to interact with your data and information. If you’ve learned any math, you’ll already be very familiar with how operators work. Kind of like having the ability to add more money into your bank account; operators give you the ability to do that. In this video, we will talk more in depth about what operators are and how to use them in your code.
Operators are one of the basic ways that you’ll tell a computer to interact with your data and information. Before we dive into operators, let’s talk about statements and expressions.
Statements and Expressions
Every piece of code you write is going to be either a statement or an expression. A statement is a single line of instructions given to the computer that results
in the computer taking action. As you’ve been following along with our videos, you’ve seen many examples of statements. Almost everything we’ve done so far has been a statement, like initializing a variable. As we put data into a variable, a specific identifier that we can access later, this is a statement. Nothing more needs to happen until we provide further instructions.
An expression is different. An expression is basically a question that we’re going to feed into the computer and ask it to find an answer for us. Math equations are a good example of expressions. For example, five plus three. If it were to take that expression and feed it into the computer, it needs to find an answer for me. In this case, eight.
What are Operators?
Operators are the symbols we use to make the computer provide answers. When we use an operator symbol we’re saying, “This is an expression.” They are how we tell the computer that we want it to transform, calculate, or do something with the information, instead of simply storing it.
It’s like math…
If you’ve learned any math, you’re already very familiar with how operators work. When you see plus, minus, multiplication, or division in that equation, you know that you need to take one side and use it to modify the other side of that operation. It works the same way in code when we do plus, minus,
multiplication. We’re going to add, subtract, and modify numbers accordingly, just like we would in math. Just like expressions, in our code it’s going to provide the right resulting answer.
In programming, operators are a very useful tool. They allow us to perform all sorts of different kinds of math, like adding more money into our bank account when we deposit. They can also be used for other operations. For example, a bank might use them to make sure that you don’t withdraw more money once your balance hits zero. We’re going to dive more into the specifics of operators
What I’d like you to take away from this video is that operators are kind of the
fundamental mover within the code. They are the way that we’re going to take many small pieces and evaluate them, combine them, or modify them into a bigger hole or a bigger result. Let’s move along and dive into some practice code.
So, now that we have eight, we can do this a little differently. Instead of doing num equals num plus one, I can just do plus plus. Console that log and I’m still getting eight. Do that two more times, and now I get 10. Plus plus is a shortcut that gets the current value of a number and adds one.
We also have a minus minus. So, if I do num minus minus, I get six because seven gets the current value of num, subtracts one, takes us down to six. And we console log six. We can also do plus equals, if we want to do values other than one. So, I could do seven plus equals seven, and that is going to give me 14. I can do minus equals, and that would give me zero. I also have times equals … and divided by equals. Those are all going to give me different values. They are going to take the original number, use it, as the left side … so this is doing num, or seven, divided by two, which gives … and then, it updates num, and that is why I’m seeing three point five over here on the side.
An important thing to know with plus plus and minus minus, is that you can put plus plus in the front, or in the … beginning of a variable to modify it. If they are on a single line like this, they are going to operate identically. But, if we put them inside of something, they’ll behave differently.
[cta id=”589″ vid=”0″]