3

I am looking for a quick way of defining several variables at once in GEE. My code currently is written like this:

var a = 0;
var b = 0;
var c = 0;
var d = 0;

which takes up space uselessly and is quite repetitive. I found that in javascript it is possible to write it as:

var [a,b,c,d] = [0,1,2,3]

but trying that in GEE only produces an error:

(SyntaxError: Unexpected token (1:5)).

1 Answer 1

5

Earth Engine does not currently support JavaScript syntax newer than “ES5”.

To define and initialize multiple variables, you can write

var a = 0, b = 0, c = 0, d = 0;

or

var a, b, c, d;
a = b = c = d = 0;

However, note that wanting to work with variables “in bulk” is often a sign that you should be using a data structure instead — a single variable holding a list/array/dictionary instead of several variables holding similar items — or a function that you can call several times. This isn't always a problem, but it's something to pause to consider any time you find yourself writing repetitive code.

1
  • I will think about the data structure. Thanks!!
    – M. Nicolas
    Commented Feb 16, 2021 at 21:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.