Springe direkt zu Inhalt

Basic JavaScript in GEE

The Code Editor integrated into the GEE works with JavaScript, a common programming language. Don’t be scared though if you have never been in contact with programming before, as it is really fun and rather easy to pick up for our geospatial applications. Just as with spoken languages, the basic patterns and principles of JavaScript will become familiar very soon just by working with it.

The following course will teach you some of the most basic code to access and filter raster and vector data directly from the GEE Data Catalog, different ways to visualize it and how you can download your freshly processed product.

Defining variables

Defining variables:

Variables can be defined by calling var x = y. Variables can store all kinds of information, like strings, numbers, functions or objects like raster and vector data.

The following example creates variables named text1 and text2, which contain the string Hello World, as well as a variable containing the number 42. Strings are a sequence of characters which can be used to implement written text in your code, for example for naming objects, and are always initiated and terminated by ‘ or “ quotes.

var text1 = 'Hello World';
var text2 = "Hello World";

var number = 42;

Print to the console tab

This will print any defined variable or object to the Console tab in the upper right panel. In the first example Hello World will appear, with no additional functionalities, as we only called a simple string. In the second example, by adding a string as an additional argument, we can add a description to the print result. This will be essentially useful when working with large amounts of data in one script.

As of now, we can not do anything fancy in the Console tab, as we only added simple strings and numbers. When we print actual data to our Console tab, as we will do when we deal with sensor data later, we will be able to check detailed information on the data as well.

print(text1);
print(text2, 'This is a text');
print(number, 'This is a number');

Comment code

Lines that are initiated with // will not be run when you execute your script. This can be essentially useful when you are testing your script, as unnecessary steps that take a long time to process can be skipped without having to delete that specific snippet of code.

You can also comment whole sections of your script by initiating with /* and terminating with */. This is essentially useful when you want to skip larger parts of your script, as it can easily save you minutes of writing and erasing multiple lines of //.

Comments are also crucial for making your script accessible and easy to understand both to yourself, as well as to others. By placing // in the same line, but behind your code, you can comment this specific line of code while still running it. This does not affect the code in any way.

//This line will not be run when executing your script

/*
This is essentially useful
when you want to skip larger parts of your script,
as it can easily save you minutes of writing and erasing
multiple lines with //.
*/

//Otherwise, you would have
//to do it like this, which can be
//rather time-consuming

var comment = 'This is a comment'; //this line defines a variable
print(comment); //this line prints it to the console

Defining lists

Lists are initiated by [ ] and used to store items which can be numbers, strings or objects. You can either print your whole list to the Console, which will then show information on all the items of your list, or you can print specific items of your list to the Console. Note that the indexing starts at 0, so to call the item ‘Vegetation’ you have to call myList2[2], even though it is the third item of your list.

var myList = [1,2,3,4,5]; //creates a list with the numbers 1-5
var myList2 = ['Water', 'Soil', 'Vegetation']; //creates a list with three strings

print(myList2);  //print myList2 to the console
print(myList2[2]);  //only print the third item of the list to the console ('Vegetation')