Javascript 101
Introduction
To use GEE, you will need to write code.
In this course, we will use the GEE Code Editor, which means that we will be coding in JavaScript.
Tutorial
I made the videos below to help walk you through through some basic JavaScript syntax, data types, and methods. I broke the tutorial into a set of short videos, rather than recording one long one. You should work through all the videos in the set.
To follow along, you will need to open the GEE Code Editor in a web browser.
I recommend that you use Google Chrome when working with the GEE Code Editor.
This is the web address to the Code Editor:
https://code.earthengine.google.com/
Honestly, you might as well bookmark that page and move the bookmark to the left corner of your toolbar. You will be visiting this site a lot over the semester.
Below the video, you should find snippets for the code shown in the video. If you hover your cursor over the upper-right corner of the snippet, you should see a little button that you can click to copy the code. You can then paste the code into the Code Editor while you watch the video. My intention is to save you some time and reduce errors that come from typos.
Quick tour
New Repo, Save File
Data types (1)
Line Comment
// This is a line comment.
String
'Hello, world' // Single quotes defines a string.
Variable
// Use var keyword to define a variable to store data.
var hello = 'Hello, world!' // This variable stores a string.
Semi-colon
// End statements with semi-colons so that the editor does not complain.
var hello_again = 'Hola Mundo';
Double quotes
// You can also define strings with double quotes.
var who_dat = "Who's there?";
Parentheses
// You can pass variables to functions within parentheses.
print(hello_again);
Comma
// You can pass more than one variable separated by commas.
print(hello_again, who_dat);
Data types (2)
Number
// This variable stores a number.
var year = 2023;
List
// Square brackets defines a list.
var some_vt_towns = ['Middlebury', 'New Haven', 'Bristol'];
Index
// Use square brackets after list object to call index of items in list.
print(some_vt_towns, some_vt_towns[0]);
Methods
// Use dot notation to call methods associated with the data type.
print(some_vt_towns.sort());
Data types (3)
Dictionary
// Use curly brackets (or braces) to define dictionaries.
var midd = {
"name": "Middlebury", // Dictionaries are composed of key:value pairs.
"pop_2010": 8496,
"pop_2020": 9152
};
print('Middlebury', midd);
Key values
// Use dot notation to call the value of object key.
print(midd.name);
print("Population change", midd.pop_2020 - midd.pop_2010);
Data types (4)
Functions
// Functions can be defined as a way to reuse code and make it easier to read.
var i_love_function = function(some_string) {
return 'I love ' + some_string + '!';
};
print(i_love_function('maps'));
Modules
// Modules can be used to share functions across multiple scripts by:
// 1. storing the module as a variable,
var tool = require('users/jhowarth/eePrimer:modules/tissot.js');
// 2. calling functions in the module as methods of this variable.
tool.drawTissot();
Mercator distortion
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.