お知らせ:
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 プログラムは、このような一連のステートメントで構成されています。コードを削除せずに実行を停止するには、コードをコメントアウトします。コードをコメントアウトする方法の 1 つは、実行したくないコードの前に 2 つのスラッシュ //
を配置することです。次に例を示します。
コードエディタ(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()
にカンマで区切られた 2 つの引数が指定されると、各引数が別の行に出力されることに注意してください。
リスト
角括弧 []
を使用してリストを定義します。数値のリスト(例:)
コードエディタ(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 サーバー上の辞書に対してこれを行う方法については、後ほど説明します。
関数
関数は、一連のオペレーションをグループ化して、コードの可読性と再利用性を向上させるもう 1 つの方法です。function
キーワードを使用して関数を定義します。関数名は英字で始まり、末尾に一対の丸かっこがあります。関数は、関数に何をするかを伝えるパラメータを取ることがよくあります。これらのパラメータは、かっこ ()
の内側に記述します。関数を構成するステートメントのセットは、中かっこで囲みます。return
キーワードは、関数の出力を示します。関数を宣言する方法はいくつかありますが、ここでは次のような方法を使用します。
コードエディタ(JavaScript)
var myFunction = function(parameter1, parameter2, parameter3) {
statement;
statement;
statement;
return statement;
};
1 行ずつ見ていきましょう。最初の行では、新しい関数を作成し、変数 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 Developers サイトのポリシーをご覧ください。Java は Oracle および関連会社の登録商標です。
最終更新日 2025-07-26 UTC。
[null,null,["最終更新日 2025-07-26 UTC。"],[[["\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)."]]