ee.Number.firstNonZero
Selects the first value if it is non-zero, and the second value otherwise.
Usage | Returns |
---|
Number.firstNonZero(right) | Number |
Argument | Type | Details |
---|
this: left | Number | The left-hand value. |
right | Number | The right-hand value. |
Examples
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
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
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2023-10-06 UTC.
[null,null,["Last updated 2023-10-06 UTC."],[[["`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."]]],["The `firstNonZero` method, applied to a number (`left`), takes another number (`right`) as an argument. It returns the `left` value if it's non-zero; otherwise, it returns the `right` value. The method is available in both JavaScript and Python, operating on numerical data. Examples show that `firstNonZero` selects 5 when given (0, 5) or (5, 0) as inputs.\n"]]