ee.Number.firstNonZero

Selects the first value if it is non-zero, and the second value otherwise.

UsageReturns
Number.firstNonZero(right)Number
ArgumentTypeDetails
this: leftNumberThe left-hand value.
rightNumberThe right-hand value.

Examples

Code Editor (JavaScript)

print('First non-zero: 0, 5', ee.Number(0).firstNonZero(ee.Number(5)));  // 5
print('First non-zero: 5, 0', ee.Number(5).firstNonZero(ee.Number(0)));  // 5

Python setup

See the Python Environment page for information on the Python API and using geemap for interactive development.

import ee
import geemap.core as geemap

Colab (Python)

print('First non-zero in [0, 5]:',
      ee.Number(0).firstNonZero(ee.Number(5)).getInfo())  # 5
print('First non-zero in [5, 0]:',
      ee.Number(5).firstNonZero(ee.Number(0)).getInfo())  # 5