What you'll learn
- Create variables with
varand store strings and numbers. - Pick clear names that make code readable.
- Print values to inspect them in the Code Editor console.
- Understand how to wrap values as Earth Engine objects when needed.
Why this matters
Variables are the foundation for every script. In Earth Engine, you store images, geometries, dates, and results in variables so you can reuse them without retyping long expressions.
What Is a Variable?
A variable is a labeled container for data.
var city = 'Gainesville';
var: declare a new variable.city: the label you will reference later.=: assignment operator.'Gainesville': the value stored (a string).
Storing and printing values
var population = 140398;
var latitude = 29.6516;
var longitude = -82.3248;
print('Population:', population);
print('Lat/Lon:', latitude, longitude);
Expected output
Console lines showing the population and coordinates you stored.
Text vs numbers
Strings need quotes ('Miami'), numbers do not (25.76).
Naming variables
// Hard to read
var x = 29.6516;
var y = -82.3248;
// Clear and descriptive
var latitude = 29.6516;
var longitude = -82.3248;
var zoomLevel = 9;
Use camelCase for multiple words (studyArea). Avoid reserved words (like
var, function).
Pro tips
- Declare once, reuse often: set
var roiand pass it to filters and reducers. - Use
print()whenever you are unsure what a variable contains. - Keep variable scope tight-declare inside functions when only used there.
Important: client vs server
Plain JavaScript variables live in your browser. Earth Engine computations need ee
objects (for example, ee.Number(5) instead of 5 when used in server math).
See Client vs Server for details.
Try it yourself
- Create
myCityas a string andmyLatitude/myLongitudeas numbers. - Print them together in one
printcall. - Wrap the numbers as
ee.Numberand add 1 using.add().
Common mistakes to avoid
- Missing quotes for text:
var city = Gainesville;will error. - Spaces in names:
var study areais invalid. - Starting with a number:
var 2ndCityis invalid. - Mismatched quotes:
'Gainesville"will error.