Introduction to JavaScript for Earth Engine

  • This tutorial provides an introduction to writing Earth Engine scripts using JavaScript.

  • You will write and run JavaScript code in the Earth Engine Code Editor.

  • Basic JavaScript data types covered include strings, numbers, lists, and objects (dictionaries).

  • Functions are used to group operations and improve code readability and reusability.

This tutorial covers just enough JavaScript to get you started writing Earth Engine scripts. For more thorough JavaScript tutorials, see these Mozilla developer resources. For an introduction to programming, with examples in JavaScript, see Eloquent JavaScript. For suggestions on JavaScript coding style, see the Google JavaScript Style Guide. In this tutorial, you're going to write JavaScript in the Earth Engine Code Editor. Before getting started, use the Code Editor guide to get familiar with the Code Editor environment.

Hello World!

Time to write your first JavaScript for Earth Engine! In your Chrome browser, go to code.earthengine.google.com and copy the following into the Code Editor:

Code Editor (JavaScript)

print('Hello World!');

Click Run and observe that 'Hello world!' is printed to the Console tab. The line above is a JavaScript statement. In JavaScript, statements end in a semicolon. Earth Engine programs are made up of a set of statements like this one. You can prevent code from running without deleting it by commenting it. One of the ways to comment out code is by putting two forward slashes // before the code that you don't want to run. For example:

Code Editor (JavaScript)

// print('Hello World!');

It's good practice to put lots of comments in your code, to describe what you're trying to do. It's also good to delete commented code that doesn't do anything anymore. Both these practices will improve code readability.

Basic JavaScript data types

Strings

Using variables to store objects and primitives helps code readability. For example, a variable that stores a string object is defined by single ' or double " quotes (but don't mix them), with single quotes preferred. Make a new string and store it in a variable called greetString:

Code Editor (JavaScript)

// Use single (or double) quotes to make a string.
var greetString = 'Ahoy there!';
// Use parentheses to pass arguments to functions.
print(greetString);

Numbers

Note that variables are defined with the keyword var. Variables can also store numbers:

Code Editor (JavaScript)

// Store a number in a variable.
var number = 42;
print('The answer is:', number);

In this example, observe that when print() is given two arguments separated by commas, each argument is printed on a different line.

Lists

Define lists with square brackets []. A list of numbers, for example:

Code Editor (JavaScript)

// Use square brackets [] to make a list.
var listOfNumbers = [0, 1, 1, 2, 3, 5];
print('List of numbers:', listOfNumbers);

Lists can also store strings or other objects. For example:

Code Editor (JavaScript)

// Make a list of strings.
var listOfStrings = ['a', 'b', 'c', 'd'];
print('List of strings:', listOfStrings);

Objects

Objects in JavaScript are dictionaries of key: value pairs. Make an object (or dictionary) using curly brackets {}, for example:

Code Editor (JavaScript)

// Use curly brackets {} to make a dictionary of key:value pairs.
var object = {
  foo: 'bar',
  baz: 13,
  stuff: ['this', 'that', 'the other thing']
};
print('Dictionary:', object);
// Access dictionary items using square brackets.
print('Print foo:', object[