Supplies fallback content when the sequence contains no elements.
Type of elements within the source iterable.
source
The source iterable.
Optional
Optional value returned in a singleton sequence when the source is empty. Defaults to null.
null
The original sequence when it has elements; otherwise, a singleton sequence containing the provided value.
Use this to ensure downstream operators always receive at least one element.
const empty = [];const withDefault = defaultIfEmpty(empty, 0).toArray();console.log(withDefault); // [0]const numbers = [1, 2, 3];const withDefault2 = defaultIfEmpty(numbers, 0).toArray();console.log(withDefault2); // [1, 2, 3] Copy
const empty = [];const withDefault = defaultIfEmpty(empty, 0).toArray();console.log(withDefault); // [0]const numbers = [1, 2, 3];const withDefault2 = defaultIfEmpty(numbers, 0).toArray();console.log(withDefault2); // [1, 2, 3]
Supplies fallback content when the sequence contains no elements.