Comparison & Logical Operators in After Effects

In this post, you will learn about Comparison & Logical Operators in After Effects. So without any further ado, let’s dive right in!

By the way, I highly recommend that you first check out my if/else statements post before diving into Comparison & Logical Operators in After Effects. Prior knowledge of if/else statements will help you get more out of this post.

Alright so for this setup I have a rotation and opacity soloed for the UKRAMEDIA.COM logo shape layer.

Opacity and Rotation Soloed in After Effect

I also have an expression inside of opacity.

Opacity Expression in After Effects

The first portion of the expression is just a comment. That is why it starts with /* and ends with */.

So it doesn’t really do anything. Basically, it is there just to show you visually what the Comparison & Logical Operators are.

/*
COMPARISON OPERATORS
== Equal To
!= Not Equal
> Greater Than
>= Greater Than Or Equal To
< Less Than
<= Less Than Or Equal To

LOGICAL OPERATORS
&& and
|| or
! not
*/

Comment After Effects Expressions

We will be plugging these Comparison & Logical Operators inside of our if-else statement below throughout this post.

if () {
100
} else {
0
}

if else statement in After Effects

Comparison Operator: Equal To ==

The first Comparison Operator is Equal to ==. So inside of our if/else statement I am going to say if rotation equal to 40, then I want the opacity to be 100% and if it is not equal to 40, I want the opacity to be 0% like so:

if (transform.rotation == 40) {
100
} else {
0
}

If rotation statement in After Effects if rotation equals to 40 in After Effects

Let’s try it out. So right now my rotation is set to 0 and because of that, the opacity is also set to 0.

opacity and rotation zero percent After Effects

However, if I rotate the UKRAMEDIA.COM logo up to +40, the opacity will change to 100%.

logo opacity set to 100 percent in After Effects

Comparison Operator: Not Equal !=

The Not Equal != Comparison Operator does the opposite of Equal ==. So if I say the rotation doesn’t equal != to +40. So if the rotation is not +40, I want the opacity to be 100% and if it is +40, I want the opacity to be 0%.

if (transform.rotation != 40) {
100
} else {
0
}

Not equal Comparison Operator in After Effects

Right now the rotation is +40 and it is giving me 0% opacity.

opacity after effects

But if the rotation doesn’t equal +40, it will give me 100% opacity.

Does not equal to 40 rotaion in After Effects

Comparison Operator: Greater Than >

Next Comparison Operator is Greater Than. For this example, I am going to say if the rotation is greater than +40, then I want the opacity to be 100%, if not, I want the opacity to be 0%.

if (transform.rotation >40) {
100
} else {
0
}

Greater than Comparison Operator in After Effects

Right now the rotation is greater than +40, so the opacity is at 100%.

Opacity is at 100 percent in After Effects

But if we go below +40, the opacity will change to 0%.

Rotation below 40 in After Effects

But here’s the thing. If I set my rotation to +40, the opacity will change to 0% because +40 is not greater than +40 but that’s where this Greater Than or Equal To comes in handy.

Rotation at 40 in After Effects

Comparison Operator: Greater Than or Equal To >=

So I am going to say if the rotation is Greater Than or Equal To +40, the opacity is going to be 100%.

if (transform.rotation >=40) {
100
} else {
0
}

Greater than or equal to 40 in After Effects

If our rotation is at +40 or above, the opacity will remain at 100%.

Rotation 40 or above is going to be 100 percent opacity in After Effects

Comparison Operator: Less Than <

The next Comparison Operator is Less Than. Now I am going to say if rotation is less than +40, I want the opacity to be 100%.

if (transform.rotation <40) {
100
} else {
0
}

Less Then Comparison Operator in After Effects
Right now the rotation is not less than +40 that is why the opacity is set to 0%.

Rotation is not 40 in After Effects

But if I go below +40, the opacity will change to 100%.

Opacity changed to 100 percent in After Effects

Comparison Operator: Less Than or Equal To <=

The same is with Less Than or Equal To. If I say the rotation is less than or equal to +40 and then the opacity will be at 100%.

if (transform.rotation <=40) {
100
} else {
0
}

Less Than or Equal To Comparison Operator in After Effects

That is it for Comparison Operators and now let’s go over Logical Operators.

Logical Operator: And &&

For this example, I will say if the rotation property is Greater Than +40, I want the opacity to be 100%.

if (transform.rotation >40) {
100
} else {
0
}

Greater Than 40 opacity 100 percent in After Effects

Right now the rotation property is Greater Than +40 and, therefore, the opacity is at 100%.

Rotation is greater than 40 in After Effects

Let me show you how to incorporate our && Logical Operator with this expression. So I can continue and say and or && if rotation is less than or smaller than +60, I want the opacity to be 100%.

if (transform.rotation >40 && transform.rotation < 60) {
100
} else {
0
}

&& Logical Operator After Effects

For example, right now our opacity property is going to be 100% between +40 and +60.

So if the rotation is +30, the opacity will not be 100%, but as soon as I go past +40, the opacity will change to 100%. If I pass +60, the opacity will change back to 0%.

That is how you can use && Logical Operator.

Logical Operator: Or ||

The next Logical Operator is or ||. So I am going to say if the rotation property is equal to +40 or || rotation property is equal to +50, or || rotation property is equal to +60. I can keep going with these or statements. You can have as many as you want.

if (transform.rotation ==40 || transform.rotation == 50 || transform.rotation == 60) {
100
} else {
0
}

So now, anytime the rotation property is +40, +50, or +60, the opacity will be 100%. If not, the opacity will be 0%.

Or Logical Operator in After Effects

So that was Or || Logical Operator but the next one is Not. Let me show you what it does.

Logical Operator: Not !

First, I am going to set a variable for this example. I will say X you are going to equal to +40, and inside of the parenthesis, I am going to say X is greater than +40 and be 100% opacity.

x = 40;
if(x > 40){
100
} else {
0
}

Not Logical Operator in After Effects

Now I know X is not bigger than +40, so the opacity will not be 100%. It is going to be a false statement. It going to be 0.

The Not Logical Operator basically reverses our statement. It will make a true statement false and if you have a false statement, the Not Logical Operator will make it true.

So right now we have a false statement because X is not bigger than +40.

To add a Not Logical Operator, we first need to add extra parenthesis to the if statement and then say if then put Not in front of it. So instead of the outcome being 0, it will now be 100% because the Not Logical Operator reverses everything.

x = 40;
if(!(x > 40)){
100
} else {
0
}

Not Logical Operator in After Effects Example

I hope you found this post useful. If you would like to learn more about Expression and how it can speed up your workflow in After Effects, then check out my Learn After Effects Expressions Course.

 

Download Project File

[sociallocker]

Download Tip 34 Project File
[/sociallocker]

About the author:

Sergei Prokhnevskiy

Sergei Prokhnevskiy is a Charlotte, NC based motion graphics designer, an online tutor, an entrepreneur, and Adobe MAX Master Speaker. Sergei is known for his quick and practical After Effects tips and tricks on YouTube, where he generated well over 6 million views on his tutorials. He has authored an online video course on After Effects Expressions and developed scripts for After Effects. He is also a former senior motion graphics designer at Fox Sports where he worked on graphics for NFL, Super Bowl, MLB, World Series, College Football, NASCAR, Women’s World Cup, and NASCAR Race Hub. He is very passionate about learning and teaching others.

5 FREE TIME-SAVING TOOLS

Over 100,000 downloads & counting!

These 5 After Effects tools will seriously speed up your workflow and save you hours of wasted time. More than that, they could be the difference between being able to complete a project on time or not!