Operators
Operators are the same thing as arithmetic operators (+, -, *, /) and can be use to create equations in a script. Lets create a simple script using variables and operators.
var myNum = 6; |
1. We'll need three parts to create
this equation:
Begin by defining the two variables. |
|
| |
||
var myNum = 6; |
2. Now we need to type in an equation.
For starters, lets create one that adds the two variables.
Begin by creating a variable. We'll call this variable "answer". |
|
| |
||
var myNum = 6; |
3. We now need to define what the sum variable is. Simply type in the first variable (myNum) followed by the operator (+) and finally the second variable (myNum2). | |
| |
||
var myNum = 6; |
4. Lets test the variable by creating an alert pop-up with the variable. To do this, type in alert(), add the answer variable inside the parenthesis and type a ; to instruction the instruciton. | |
| |
||
![]() |
5. Save your file as a *.jsx file on your desktop. You can name it whatever you want. Open Photoshop and run the file (File> Scripts> Browse). an alert pop-up should popup with the answer to 6+4. | |
FAQ:
Q: What happens when you try to add strings instead of numbers?
A: JavaScript will try to convert the string to a number. For example, if your string is "5", it'll
recognize that it's the number 5. However, if your string is "myString", it'll return an error with
the NaN value which stands for "Not a Number".
Tip:
When only adding or subtracting one (ex. myVar + 1), you can simply type in "myVar ++" to increase "myVar" by
1 or "myVar decrease to decrase "myVar" by 1. This works the same as "myVar + 1" or "myVar - 1". Using
increment or decrement will make your code cleaner and easier to read.





More Photoshop: