Comments are notes for humans. They do not run, but they make scripts easier to read, debug, and share. Use them to explain intent and organize steps.
Learning objectives
- Write single-line (
//) and multi-line (/* */) comments. - Use comments to explain why choices were made, not just what code does.
- Apply comments to organize sections in longer scripts.
- Use keyboard shortcuts to toggle comments quickly.
Why it matters
Good comments help classmates, instructors, and your future self understand a script. They also speed up debugging by letting you disable lines temporarily.
Comment syntax
// Single-line comment
/*
Multi-line comments are great
for longer explanations.
*/
Example with Earth Engine code
// 1. Define study area (Gainesville, FL)
var geometry = ee.Geometry.Point([-82.3248, 29.6516]);
// 2. Load Landsat 8 surface reflectance for 2023
var image = ee.ImageCollection('LANDSAT/LC08/C02/T1_L2')
.filterBounds(geometry) // filter by location
.filterDate('2023-01-01', '2023-12-31') // filter by date
.first(); // get the first image
// 3. Inspect
print('First image', image);
Keyboard shortcuts (Code Editor)
- Windows/Linux: Ctrl + /
- Mac: Cmd + /
Select multiple lines and toggle them together. This is handy for A/B testing sections of code.
Good vs not-so-good
- Good:
// Use median composite to reduce clouds - Good:
// Buffer by 1000 m to include surrounding context - Not helpful:
// Set x to 5when the code already saysvar x = 5;
Try it: comment for clarity
Add a short header (what, where, when) to your next script.
Label three major steps with divider comments (load, process, visualize).
Temporarily comment out one Map.addLayer line to compare two visualizations.
Common mistakes
- Describing code literally instead of explaining intent.
- Leaving outdated comments that no longer match the code.
- Commenting sensitive credentials instead of using environment variables (never store secrets in code).
Quick self-check
- How do you start a multi-line comment-
- What makes a comment useful rather than redundant-
- Which shortcut toggles comments in the Code Editor-
Next steps
- Functions to structure logic you describe with comments.
- Client vs Server to avoid common execution mistakes you might annotate.