When learning any language, one of the most important concepts is the language's syntax,
which governs grammar and sentence structure.
Just like spoken languages, the expression language uses a syntax system,
which is a set of guidelines for building simple scripts and complex functions.
The After Effects Expression Language is based on a subset of JavaScript 1.2.
It uses a left-to-right dot notation format, separating attributes with period operators.
It's also a case-sensitive language, so you must use proper capitalization when typing out
objects, attributes, and functions.
Spaces and line breaks within the code are called whitespace and are usually ignored
when an expression is evaluated.
There are occasional situations where whitespace matters, like text strings,
but the primary function of whitespace is for legibility and formatting.
Take a look at this example expression for Scale.
This crowded one-line expression sets a variable, and then builds a math operation
using the variable in the equation.
There’s a lot going on in the middle with the semi-colon, parentheses,
and multiplication operator, and it’s a little difficult to parse.
Now, take a look at the revised version with spaces and a line break.
This is the exact same expression, the only difference is added white space
around certain elements.
It’s much easier to read and understand what’s going on after a quick glance at the code.
A semicolon is used to keep statements separated.
Semi-colons are required between multiple statements on a single line.
In this one-line example, a semi colon is missing, and will return an error because
the variable “a” is missing a semi-colon between statements and is not properly defined.
This example includes a semi colon, and it correctly returns a value of 50.
Leaving comments around sections of your code can serve as a reminder for how it works.
If you are sending the project to someone and they need to edit the expression,
comments are useful.
In the first example, the comment is initiated with a double forward slash
and continues to the end of the line.
The second example shows how comments can be wrapped in a set of forward slashes
and asterisks, to be placed almost anywhere.
Everything past the forward slashes to the end of the line, or wrapped in the
forward slash asterisks are excluded from evaluation, even snippets of code.
So here’s a tip: If you’re working in a complex expression
and need to temporarily disable a line of code, it’s much easier to comment the code out
rather than delete it.
This way, you can still see, and access, the code without it affecting your final expression.
When building a multi-line expression, there are a few good practices you can employ
to keep your code clean and organized.
It's all up to your personal preference, use these as basic guidelines.
Add whitespace to increase legibility.
It's most effective around your math operations.
Use semi-colons to end statements.
It’s a good habit to get into to avoid errors.
Include helpful comments to easily understand expressions later.
Link properties to variables.
You don't want to pick whip the same property over and over.
Create a variable and reference it.
We've arrived at the end of Lesson 502:
An Overview of the Structure and Formatting of Expressions
If you enjoy this expressions course, consider purchasing the paid content.
It includes in-depth documentation, extra tutorial content, high definition videos,
and all the project files used in the training.
Your purchase will help to create more free courses like this in the future.