The Arcade developer docs talk about "increment operators":
The increment/decrement by one operators have both a pre and post versions that differ in what they return. The pre increment version adds one to the variable and returns the new value while the post increment adds one and then returns the initial value of the variable.
//Pre increment example
var x=10
++x // x is now 11 and the value 11 is returned
//Post increment example
var x=10
x++ // x is now 11 but the value of 10 is returned.
Question:
What is an example of using x++ in an Arcade expression? (outside of a for loop)
I don't think I understand why we'd want to use it: "x is now 11 but the value of 10 is returned". Why would we want to return the "initial value of the variable" (10)?
Related: Why avoid increment ("++") and decrement ("--") operators in JavaScript?
Thanks!
Two basic examples:
Counting
var interesting_features = 0
for(var f in feature_set) {
// do something with every feature
// ...
// only count interesting features
if(f.IsInteresting == 1) {
interesting_features++ // the same as interesting_features += 1
}
}
Legacy Appending
var arr = []
// Previously, you had to append elements like this
var i = 0
arr[i++] = "a"
arr[i++] = "b"
// Now, you can also do it like this:
Push(arr, "a")
Push(arr, "b")
Why would we want to return the "initial value of the variable"
For example, to implement a stack:
var i = 0
var stack = []
function stack_push(val) {stack[i++] = val}
function stack_pop() {return stack[--i]}
stack_push(1)
stack_push(2)
stack_push(3)
Console("stack: " + stack)
Console(stack_pop())
Console(stack_pop())
stack_push("a")
Console("stack: " + stack)
stack: [1,2,3] 3 2 stack: [1,"a",3]