Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
A unary operator is a single operator that can affect the resulting value of the variable.
Note
The NOT operator which is denoted by a ! also called a bang is used for Boolean values or expressions.
Further Reading

0:00
We learned about binary operators, which works with two targets, or operands.

0:05
Simply put, two numbers, variables or constants, on either side of the operator.

0:11
Whereas the unary operator works with a single target.

0:16
So let's find out how to work with unary operators.

0:20
So, I've created a new playground here and I've named it Unary.

0:25
So let's imagine that we're creating a game that wants to

0:27
keep track of the score for the players.

0:32
And we have two sets of scores.

0:34
Let's focus on the score for levels.

0:36
This game has multiple different levels and

0:38
we want to keep track of the level score.

0:41
So we'll create a variable called levelScore and we'll initialize that to 0.

0:48
Cuz we can't just create a variable and not provide it with an initial value, cuz

0:54
it needs to know whether it's an integer or a double or a float or a Boolean.

0:59
Next thing is that it's important that this is a variable because we

1:03
want to update its value cuz it's not going to be a constant.

1:08
So, each time a player shoots down an enemy,

1:11
we want to reward them with a point.

1:14
And for that, we have to increment the level score by one.

1:18
So, in order for us to increment the level score by one, we can simply do this.

1:22
We can say levelScore is equal to levelScore plus 1.

1:28
But this might be a bit confusing for you to see levelScore twice.

1:31
You have it here and here.

1:34
But essentially what we're doing is we're saying, like, take

1:38
the current value of levelScore, add 1 to it, and then assign it back to levelScore.

1:44
So now in the next line,

1:45
if I print out levelScore, you will see in the results pane that it says 1.

1:50
So this is great.

1:53
But unary operators give us the opportunity to

1:57
condense this into one single expression.

2:00
So I can get rid of this whole expression here and simply say levelScore plus plus.

2:07
And as you'll see in the next line, it's already incremented to 1.

2:12
So, we took that whole line of code and distilled it down to this.

2:18
Moreover, let me go ahead and delete this.

2:22
We can create another variable called totalScore and

2:26
then essentially, we can either initialize that to 0 and on the next line,

2:32
we can say that totalScore is equal to levelScore plus plus.

2:38
So, what are we doing here?

2:40
If I print out the value of totalScore, you will see that it says 1.

2:44
And if I print out the value of levelScore, you will see that it says 2.

2:52
So here, we've already incremented levelScore, which makes it 1.

2:57
And now we're incrementing it once again, but when we're assigning that to

3:01
totalScore, it gives us this previous value, not the new value.

3:07
So, what is going on?

3:09
Well, unary operators act as prefix and postfix operators.

3:14
Here, you will notice that the plus plus, or

3:17
the unary operator, is post the name of the variable.

3:21
That's called a postfix operator.

3:24
Now, if I move the plus plus prefix, which means before the name of the variable,

3:30
what it will do is it will first increment the value of levelScore and

3:36
then assign it to totalScore.

3:38
And that's why in the results pane,

3:40
you will see that both of them now have the value of 2.

3:44
So it's important where you place this unary operator.

3:48
Of course, if you're simply just incrementing the value of levelScore by

3:52
itself, then it doesn't matter where you place it

3:55
because you're simply saying increment the value of this variable.

3:59
And you're not really assigning that value back to anything.

4:02
Now, once again here, I could combine these two statements into one.

4:08
So, I could essentially take levelScore and put it here in place of 0.

4:13
Let me get rid of this.

4:17
So now you have initialized totalScore and incremented levelScore all in one line.

4:26
So, just like we have the plus plus, or

4:29
increment operator, we have the minus minus.

4:33
So, let me replace this plus plus with minus minus.

4:37
And on the next line,

4:38
if I print out levelScore, you will see that it shows as minus 1.

4:43
So, instead of incrementing level score by 1, we're decrementing it by 1.

4:49
Here, imagine that our player took a hit from an enemy ship, so

4:53
we wanna decrement their score, and that's how we would do it.

4:56
We would just say levelScore minus minus.

4:59
And similarly, here we can do minus minus levelScore

5:06
as a prefix or we can do minus minus levelscore as a postfix.

5:14
So, so far, we have learned about the plus plus, or the increment op, unary operator.

5:22
We have learned about the minus minus, or the decrement unary operator.

5:28
Next, we have the negative operator, and this essentially

5:35
what it does is it takes a positive value and turns it into a negative value.

5:42
So, if I'm gonna take levelScore, which was minus 2, and if I prefix

5:49
it with the minus operator, it essentially just turned it into a positive number.

5:54
So, take whatever number you have and, you know, negate that,

5:58
or multiply that by minus 1.

6:00
So this would be essentially doing the same as minus 1.

6:05
So that's, that's the negative operator.

6:08
It just multiplies it by negative 1, so

6:10
whatever the value is, it's gonna be the opposite of that.

6:13
So if it's positive, it'll be negative.

6:15
If it is negative, it will turn into positive.

6:20
So that's our negative operator.

6:24
And finally, we have the NOT operator.

6:27
[BLANK_AUDIO]

6:31
And the NOT operator simply negates the value.

6:35
So let's say you have a constant called on, which has the value of true, and

6:41
you wanted to create another constant called off, but you wanted to give it

6:45
the opposite value of on, so you would do bang, or exclamation mark, on.

6:52
And now you'll see in the results pane, off has the value of false.

6:58
Similarly, if I turned on to false, off would have the value of true.

7:04
So it simply negates whatever value that you have in that variable or constant.

7:10
These are fairly straightforward and

7:12
you will find that you use them quite frequently when programming,

7:15
especially the NOT operator because they're are several use cases for it.

7:21
We've learned a lot throughout this stage, a lot of the basic math operators,

7:26
their precedence, and even unary operators.

7:29
In our next stage, we'll explore a new type called arrays and dictionaries.
You need to sign up for Treehouse in order to download course files.
Sign up