ee.Number.firstNonZero

  • firstNonZero() is a method that selects and returns the first number if it is not zero, otherwise, it returns the second number.

  • It takes two arguments: the left-hand value (this: left) and the right-hand value (right), both of which should be of type Number.

  • This function can be useful for conditional selections where a fallback value is needed if the primary value is zero.

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