Announcement: All noncommercial projects registered to use Earth Engine beforeApril 15, 2025mustverify noncommercial eligibilityto maintain access. If you have not verified by September 26, 2025, your access may be on hold.
Introduction to JavaScript for Earth EngineStay organized with collectionsSave and categorize content based on your preferences.
AI-generated Key Takeaways
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 enoughJavaScriptto get you started writing Earth Engine scripts. For more thorough JavaScript tutorials,
seethese Mozilla developer
resources. For an introduction to programming, with examples in JavaScript, seeEloquent JavaScript. For suggestions
on JavaScript coding style, see theGoogle JavaScript Style
Guide. In this tutorial, you're going to write JavaScript in the Earth EngineCode Editor. Before getting started,
usethe Code Editor guideto get familiar
with the Code Editor environment.
Hello World!
Time to write your first JavaScript for Earth Engine! In your Chrome browser, go tocode.earthengine.google.comand copy
the following into theCode Editor:
ClickRunand observe that 'Hello world!' is printed to theConsole 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:
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), withsingle quotes
preferred. Make a new string and store it in a variable calledgreetString:
Code Editor (JavaScript)
// Use single (or double) quotes to make a string.vargreetString='Ahoy there!';// Use parentheses to pass arguments to functions.print(greetString);
Numbers
Note that variables are defined with the keywordvar. Variables can also
store numbers:
Note that you can get a value from a dictionary by supplying the key. This example
shows you how to do that for JavaScript objects. Later you'll learn how to do it for
dictionaries that are on the Earth Engine server.
Functions
Functions are another way to improve code readability and reusability by grouping sets
of operations. Define a function with thefunctionkeyword. Function names
start with a letter and have a pair of parentheses at the end. Functions often takeparameterswhich tell the function what to do. These parameters go inside the
parentheses(). The set of statements making up the function go inside curly
brackets. Thereturnkeyword indicates what the function output is. There
are several ways to declare a function, but here we'll use something like this:
Let's consider the lines one by one. The first line creates a new function and assigns
it to the variablemyFunction. This variable could have been named
anything. It defines how to call the function later. The terms in the parentheses after
the function name (i.e. parameter1, parameter2, parameter3) are the parameter names and
could have been named anything as well, though it's good practice to give them unique names
that are different from the code outside the function. Whatever you name them, these are
the names that function will use to refer to the values that are passed into the function
when it is called. The value of a parameter once it's been passed into a function is
called anargument. Although functions can use variables declared outside
the function (globalvariables), function arguments are not visible outside the
function. Functions can take as many parameters as you need, even zero. Here's a simple
example of a function that just returns its argument:
This is an example of a user-defined function. There are also lots of built-in Earth
Engine functions. Explore the Code EditorDocs
tabto learn about these built-in functions. Here's a very simple example of an
Earth Engine function:
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2023-10-06 UTC."],[],["This tutorial introduces basic JavaScript concepts within the Earth Engine Code Editor. Key actions include writing and running code, like `print('Hello World!');`, using comments (`//`), and defining variables with `var`. It covers data types such as strings (using quotes), numbers, lists (using `[]`), and objects (using `{}`). Functions are explained using `function`, with parameters and the `return` statement. It also highlights built-in Earth Engine functions and the location of their documentation.\n"]]