Retrieves the element at the specified zero-based index or returns null when the index is out of range.
null
Type of elements within the source iterable.
source
The source iterable.
Zero-based position of the element to retrieve.
The element at index, or null when source is shorter than index + 1 or when index is negative.
index
index + 1
Use this overload when out-of-range access should produce a sentinel value instead of throwing an exception.
const numbers = [1, 2, 3, 4, 5];const element = elementAtOrDefault(numbers, 2);console.log(element); // 3const element2 = elementAtOrDefault(numbers, 10);console.log(element2); // null Copy
const numbers = [1, 2, 3, 4, 5];const element = elementAtOrDefault(numbers, 2);console.log(element); // 3const element2 = elementAtOrDefault(numbers, 10);console.log(element2); // null
Retrieves the element at the specified zero-based index or returns
nullwhen the index is out of range.