Google Earth Engine – How to Concatenate Strings in Google Earth Engine

google-earth-enginegoogle-earth-engine-javascript-api

I'm using the Javascript console on Google Earth Engine, and I'm stumped by a simple problem. I want to concatenate and print two values on one line. One of them is an object property.

When I use a comma to separate the parts in the print statement, I get the output I want, except that it is printed on separate lines. When I use a plus ('+'), the object's toString method is invoked and I get vastly more output than I want.

var img = ee.Image(img_list.get(0)) //grab an image
var date_index = img.get('system:index'); //get its date index property

print('MOD13Q_', date_index);
//Output (basically what I want, except on two lines instead of one)
//'MOD13Q_'
//'2018-01-17' 


print('MOD13Q_' + date_index); //Oops, '+' invoked the toString method
//'MOD13Q_ComputedObject({
  "type": "Invocation",
  "arguments": {
    "object": {
      "type": "Invocation",
      "arguments": {
        "list": {
          "type"....etc.

Is there any way to override the toString method, or to prevent it from being invoked? This question describes something similar, but does not show how to solve my problem.

Best Answer

Use server-side string method cat instead of JavaScript concatenation operator +.

var outStr = ee.String('MOD13Q_').cat(date_index);
print(outStr);

// OUT: 'MOD13Q_2018-01-17'
Related Question