I wrote some simple code to test the if/else structure in GEE:

var flag = ee.Algorithms.IsEqual(1,5);
if (flag) {
 print("this is true");
} else {
 print("this is false");

the output was:

this is true

Does anyone know how to adjust the above statement to be effective?

  • Note that you are using a server-side Earth Engine object in a client-side operation. Earth Engine objects in the client are interpreted as a JSON strings and strings are converted to boolean in JavaScript if() if there is no expression to evaluate. Since the JSON string is not null it converts to true. More on Client vs Server, More on JS if() and strings. – Justin Braaten Dec 4 '19 at 20:17
  • The EE code editor print() function hides some of this semantic. Your print(flag) outputs false, but like Justin points out, it's not a boolean. Look here: code.earthengine.google.com/f4801a5b13115c04486c1785c273ce43 At the time you do your if(flag), the value of flag is actually not evaluated yet. – Daniel Wiell Dec 4 '19 at 20:42
  • Good point, Daniel, thanks for noting: The EE code editor print() function hides some of this semantic – Justin Braaten Dec 4 '19 at 20:54

You typically try to not end up in situations where you do conditionals in EE. If you really, really need it, you can do like this:

var flag = ee.Algorithms.IsEqual(1, 5)
var s = ee.Algorithms.If(flag, "this is true", "this is false")


  • 1
    See the If/Else section of the Earth Engine developer docs for more info on why ee.Algorithms.If() should be avoided if possible. – Justin Braaten Dec 6 '19 at 23:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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