github.com/hugorut/terraform@v1.1.3/website/docs/language/expressions/operators.mdx (about) 1 --- 2 page_title: Operators - Configuration Language 3 description: >- 4 Operators transform or combine expressions. Learn about arithmetic, logical, 5 equality, and comparison operators. 6 --- 7 8 # Arithmetic and Logical Operators 9 10 An _operator_ is a type of expression that transforms or combines one or more 11 other expressions. Operators either combine two values in some way to 12 produce a third result value, or transform a single given value to 13 produce a single result. 14 15 Operators that work on two values place an operator symbol between the two 16 values, similar to mathematical notation: `1 + 2`. Operators that work on 17 only one value place an operator symbol before that value, like 18 `!true`. 19 20 The Terraform language has a set of operators for both arithmetic and logic, 21 which are similar to operators in programming languages such as JavaScript 22 or Ruby. 23 24 When multiple operators are used together in an expression, they are evaluated 25 in the following order of operations: 26 27 1. `!`, `-` (multiplication by `-1`) 28 1. `*`, `/`, `%` 29 1. `+`, `-` (subtraction) 30 1. `>`, `>=`, `<`, `<=` 31 1. `==`, `!=` 32 1. `&&` 33 1. `||` 34 35 Use parentheses to override the default order of operations. Without 36 parentheses, higher levels will be evaluated first, so Terraform will interpret 37 `1 + 2 * 3` as `1 + (2 * 3)` and _not_ as `(1 + 2) * 3`. 38 39 The different operators can be gathered into a few different groups with 40 similar behavior, as described below. Each group of operators expects its 41 given values to be of a particular type. Terraform will attempt to convert 42 values to the required type automatically, or will produce an error message 43 if automatic conversion is impossible. 44 45 ## Arithmetic Operators 46 47 The arithmetic operators all expect number values and produce number values 48 as results: 49 50 * `a + b` returns the result of adding `a` and `b` together. 51 * `a - b` returns the result of subtracting `b` from `a`. 52 * `a * b` returns the result of multiplying `a` and `b`. 53 * `a / b` returns the result of dividing `a` by `b`. 54 * `a % b` returns the remainder of dividing `a` by `b`. This operator is 55 generally useful only when used with whole numbers. 56 * `-a` returns the result of multiplying `a` by `-1`. 57 58 Terraform supports some other less-common numeric operations as 59 [functions](/language/expressions/function-calls). For example, you can calculate exponents 60 using 61 [the `pow` function](/language/functions/pow). 62 63 ## Equality Operators 64 65 The equality operators both take two values of any type and produce boolean 66 values as results. 67 68 * `a == b` returns `true` if `a` and `b` both have the same type and the same 69 value, or `false` otherwise. 70 * `a != b` is the opposite of `a == b`. 71 72 Because the equality operators require both arguments to be of exactly the 73 same type in order to decide equality, we recommend using these operators only 74 with values of primitive types or using explicit type conversion functions 75 to indicate which type you are intending to use for comparison. 76 77 Comparisons between structural types may produce surprising results if you 78 are not sure about the types of each of the arguments. For example, 79 `var.list == []` may seem like it would return `true` if `var.list` were an 80 empty list, but `[]` actually builds a value of type `tuple([])` and so the 81 two values can never match. In this situation it's often clearer to write 82 `length(var.list) == 0` instead. 83 84 ## Comparison Operators 85 86 The comparison operators all expect number values and produce boolean values 87 as results. 88 89 * `a < b` returns `true` if `a` is less than `b`, or `false` otherwise. 90 * `a <= b` returns `true` if `a` is less than or equal to `b`, or `false` 91 otherwise. 92 * `a > b` returns `true` if `a` is greater than `b`, or `false` otherwise. 93 * `a >= b` returns `true` if `a` is greater than or equal to `b`, or `false` otherwise. 94 95 ## Logical Operators 96 97 The logical operators all expect bool values and produce bool values as results. 98 99 * `a || b` returns `true` if either `a` or `b` is `true`, or `false` if both are `false`. 100 * `a && b` returns `true` if both `a` and `b` are `true`, or `false` if either one is `false`. 101 * `!a` returns `true` if `a` is `false`, and `false` if `a` is `true`. 102 103 Terraform does not have an operator for the "exclusive OR" operation. If you 104 know that both operators are boolean values then exclusive OR is equivalent 105 to the `!=` ("not equal") operator.