公告:所有在
2025 年 4 月 15 日之前注册使用 Earth Engine 的非商业项目都必须
验证是否符合非商业性质的资格条件,才能继续使用 Earth Engine。
Earth Engine 专用 JavaScript 简介
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
本教程仅介绍足够让您开始编写 Earth Engine 脚本的 JavaScript。如需更全面的 JavaScript 教程,请参阅 Mozilla 开发者资源。如需了解编程入门知识(附带 JavaScript 示例),请参阅 Eloquent JavaScript。如需有关 JavaScript 编码样式的建议,请参阅 Google JavaScript 样式指南。在本教程中,您将在 Earth Engine 代码编辑器中编写 JavaScript。在开始之前,请先参阅代码编辑器指南,熟悉代码编辑器环境。
Hello World!
是时候编写您的第一个 Earth Engine JavaScript 了!在 Chrome 浏览器中,前往 code.earthengine.google.com,然后将以下内容复制到代码编辑器中:
代码编辑器 (JavaScript)
print('Hello World!');
点击运行,然后观察到“Hello world!”打印到了控制台标签页。上面一行是 JavaScript 语句。在 JavaScript 中,语句以英文分号结尾。Earth Engine 程序由一组这样的语句组成。您可以通过注释代码来防止代码在不删除的情况下运行。注释掉代码的方法之一是在不想运行的代码前面放置两个正斜杠 //
。例如:
代码编辑器 (JavaScript)
// print('Hello World!');
最好在代码中添加大量注释,以说明您要执行的操作。此外,最好删除不再执行任何操作的注释掉的代码。
这两种做法都可以提高代码的可读性。
基本 JavaScript 数据类型
字符串
使用变量存储对象和基元有助于提高代码的可读性。例如,存储字符串对象的变量由单引号 '
或双引号 "
定义(但不要混用),最好使用单引号。创建一个新字符串并将其存储在名为 greetString
的变量中:
代码编辑器 (JavaScript)
// Use single (or double) quotes to make a string.
var greetString = 'Ahoy there!';
// Use parentheses to pass arguments to functions.
print(greetString);
Numbers
请注意,变量使用关键字 var
进行定义。变量还可以存储数字:
代码编辑器 (JavaScript)
// Store a number in a variable.
var number = 42;
print('The answer is:', number);
在此示例中,请注意,当 print()
获得两个以英文逗号分隔的实参时,每个实参都会打印在不同的行中。
列表
使用方括号 []
定义列表。一个数字列表,例如:
代码编辑器 (JavaScript)
// Use square brackets [] to make a list.
var listOfNumbers = [0, 1, 1, 2, 3, 5];
print('List of numbers:', listOfNumbers);
列表还可以存储字符串或其他对象。例如:
代码编辑器 (JavaScript)
// Make a list of strings.
var listOfStrings = ['a', 'b', 'c', 'd'];
print('List of strings:', listOfStrings);
对象
JavaScript 中的对象是 key: value
对的字典。使用大括号 {}
创建一个对象(或字典),例如:
代码编辑器 (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['foo']);
// Access dictionary items using dot notation.
print('Print stuff:', object.stuff);
请注意,您可以通过提供键从字典中获取值。此示例展示了如何针对 JavaScript 对象执行此操作。稍后,您将学习如何针对 Earth Engine 服务器上的字典执行此操作。
函数
函数是另一种通过对一组操作进行分组来提高代码可读性和可重用性的方式。使用 function
关键字定义函数。函数名称以字母开头,并以一对圆括号结尾。函数通常会接受形参,这些形参会告知函数要执行的操作。这些形参位于圆括号 ()
内。构成函数的语句集位于大括号内。return
关键字用于指明函数输出是什么。声明函数的方法有多种,但这里我们将使用如下方法:
代码编辑器 (JavaScript)
var myFunction = function(parameter1, parameter2, parameter3) {
statement;
statement;
statement;
return statement;
};
我们来逐行分析一下。第一行创建了一个新函数,并将其分配给变量 myFunction
。此变量可以命名为任何名称。它定义了稍后如何调用该函数。函数名称后面的圆括号中的字词(即 parameter1、parameter2、parameter3)是形参名称,也可以使用任何名称,不过最好为它们指定与函数外部的代码不同的唯一名称。无论您如何命名,这些名称都将是函数在被调用时用于引用传递给函数的值的名称。形参在传递到函数中的值称为实参。虽然函数可以使用在函数外部声明的变量(全局变量),但函数实参在函数外部不可见。函数可以根据需要采用任意数量的参数,甚至可以不采用任何参数。下面是一个只返回其参数的简单函数示例:
代码编辑器 (JavaScript)
// The reflect function takes a single parameter: element.
var reflect = function(element) {
// Return the argument.
return element;
};
print('A good day to you!', reflect('Back at you!'));
以下是用户定义的函数的一个示例。此外,还有许多内置的 Earth Engine 函数。您可以探索代码编辑器中的“文档”标签页,了解这些内置函数。下面是一个非常简单的 Earth Engine 函数示例:
代码编辑器 (JavaScript)
var aString = ee.Algorithms.String(42);
在下一部分中,详细了解 Earth Engine 对象和方法。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-26。
[null,null,["最后更新时间 (UTC):2025-07-26。"],[[["\u003cp\u003eThis tutorial provides a basic introduction to JavaScript for use within the Google Earth Engine Code Editor.\u003c/p\u003e\n"],["\u003cp\u003eIt covers fundamental JavaScript concepts like variables, data types (strings, numbers, lists, objects), and functions.\u003c/p\u003e\n"],["\u003cp\u003eUsers are encouraged to utilize the Code Editor's features like the Console and Docs tabs for a better experience.\u003c/p\u003e\n"],["\u003cp\u003eEarth Engine programs consist of JavaScript statements, and the tutorial provides examples for basic operations.\u003c/p\u003e\n"],["\u003cp\u003eThe tutorial serves as a starting point for learning Earth Engine, with more advanced topics covered in subsequent tutorials.\u003c/p\u003e\n"]]],["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"],null,["# Introduction to JavaScript for Earth Engine\n\nThis tutorial covers just enough\n[JavaScript](https://developer.mozilla.org/en-US/docs/Web/JavaScript/About_JavaScript)\nto get you started writing Earth Engine scripts. For more thorough JavaScript tutorials,\nsee [these Mozilla developer\nresources](https://developer.mozilla.org/en-US/docs/Web/JavaScript). For an introduction to programming, with examples in JavaScript, see\n[Eloquent JavaScript](http://eloquentjavascript.net/). For suggestions\non JavaScript coding style, see the\n[Google JavaScript Style\nGuide](http://google.github.io/styleguide/javascriptguide.xml). In this tutorial, you're going to write JavaScript in the Earth Engine\n[Code Editor](https://code.earthengine.google.com/). Before getting started,\nuse [the Code Editor guide](/earth-engine/guides/playground) to get familiar\nwith the Code Editor environment.\n\nHello World!\n------------\n\nTime to write your first JavaScript for Earth Engine! In your Chrome browser, go to\n[code.earthengine.google.com](https://code.earthengine.google.com/) and copy\nthe following into the [Code Editor](/earth-engine/guides/playground):\n\n### Code Editor (JavaScript)\n\n```javascript\nprint('Hello World!');\n```\n\nClick **Run** and observe that 'Hello world!' is printed to the\n[Console tab](/earth-engine/guides/playground#console-tab). The line above is a JavaScript\nstatement. In JavaScript, statements end in a semicolon. Earth Engine programs are made\nup of a set of statements like this one. You can prevent code from running without\ndeleting it by commenting it. One of the ways to comment out code is by putting two\nforward slashes `//` before the code that you don't want to run. For example:\n\n### Code Editor (JavaScript)\n\n```javascript\n// print('Hello World!');\n```\n\nIt's good practice to put lots of comments in your code, to describe what you're trying\nto do. It's also good to delete commented code that doesn't do anything anymore.\nBoth these practices will improve code readability.\n\nBasic JavaScript data types\n---------------------------\n\n### Strings\n\nUsing variables to store objects and primitives helps code readability. For example,\na variable that stores a string object is defined by single `'` or double\n`\"` quotes (but don't mix them), with\n[single quotes\npreferred](https://google.github.io/styleguide/javascriptguide.xml#Strings). Make a new string and store it in a variable called\n`greetString`:\n\n\n### Code Editor (JavaScript)\n\n```javascript\n// Use single (or double) quotes to make a string.\nvar greetString = 'Ahoy there!';\n// Use parentheses to pass arguments to functions.\nprint(greetString);\n```\n\n### Numbers\n\nNote that variables are defined with the keyword `var`. Variables can also\nstore numbers:\n\n### Code Editor (JavaScript)\n\n```javascript\n// Store a number in a variable.\nvar number = 42;\nprint('The answer is:', number);\n```\n\nIn this example, observe that when `print()` is given two arguments separated\nby commas, each argument is printed on a different line.\n\n### Lists\n\nDefine lists with square brackets `[]`. A list of numbers, for example:\n\n### Code Editor (JavaScript)\n\n```javascript\n// Use square brackets [] to make a list.\nvar listOfNumbers = [0, 1, 1, 2, 3, 5];\nprint('List of numbers:', listOfNumbers);\n```\n\nLists can also store strings or other objects. For example:\n\n\n### Code Editor (JavaScript)\n\n```javascript\n// Make a list of strings.\nvar listOfStrings = ['a', 'b', 'c', 'd'];\nprint('List of strings:', listOfStrings);\n```\n\n### Objects\n\nObjects in JavaScript are dictionaries of `key: value` pairs. Make an object\n(or dictionary) using curly brackets `{}`, for example:\n\n### Code Editor (JavaScript)\n\n```javascript\n// Use curly brackets {} to make a dictionary of key:value pairs.\nvar object = {\n foo: 'bar',\n baz: 13,\n stuff: ['this', 'that', 'the other thing']\n};\nprint('Dictionary:', object);\n// Access dictionary items using square brackets.\nprint('Print foo:', object['foo']);\n// Access dictionary items using dot notation.\nprint('Print stuff:', object.stuff);\n```\n\nNote that you can get a value from a dictionary by supplying the key. This example\nshows you how to do that for JavaScript objects. Later you'll learn how to do it for\ndictionaries that are on the Earth Engine server.\n\nFunctions\n---------\n\nFunctions are another way to improve code readability and reusability by grouping sets\nof operations. Define a function with the `function` keyword. Function names\nstart with a letter and have a pair of parentheses at the end. Functions often take\n*parameters* which tell the function what to do. These parameters go inside the\nparentheses `()`. The set of statements making up the function go inside curly\nbrackets. The `return` keyword indicates what the function output is. There\nare several ways to declare a function, but here we'll use something like this:\n\n### Code Editor (JavaScript)\n\n```javascript\nvar myFunction = function(parameter1, parameter2, parameter3) {\n statement;\n statement;\n statement;\n return statement;\n};\n```\n\nLet's consider the lines one by one. The first line creates a new function and assigns\nit to the variable `myFunction`. This variable could have been named\nanything. It defines how to call the function later. The terms in the parentheses after\nthe function name (i.e. parameter1, parameter2, parameter3) are the parameter names and\ncould have been named anything as well, though it's good practice to give them unique names\nthat are different from the code outside the function. Whatever you name them, these are\nthe names that function will use to refer to the values that are passed into the function\nwhen it is called. The value of a parameter once it's been passed into a function is\ncalled an *argument* . Although functions can use variables declared outside\nthe function (*global* variables), function arguments are not visible outside the\nfunction. Functions can take as many parameters as you need, even zero. Here's a simple\nexample of a function that just returns its argument:\n\n### Code Editor (JavaScript)\n\n```javascript\n// The reflect function takes a single parameter: element.\nvar reflect = function(element) {\n // Return the argument.\n return element;\n};\nprint('A good day to you!', reflect('Back at you!'));\n```\n\nThis is an example of a user-defined function. There are also lots of built-in Earth\nEngine functions. Explore the Code Editor [Docs\ntab](/earth-engine/guides/playground#api-reference-docs-tab) to learn about these built-in functions. Here's a very simple example of an\nEarth Engine function:\n\n### Code Editor (JavaScript)\n\n```javascript\nvar aString = ee.Algorithms.String(42);\n```\n\nIn the next section, learn more about [Earth Engine Objects\nand Methods](/earth-engine/tutorials/tutorial_js_02)."]]