Skip to content

atOrDefault(index, defValue)

suckgamoni edited this page May 14, 2013 · 2 revisions

Returns the element at a specified index in a sequence or a default value if the index is out of range.

Syntax

Parameters

index

Type: number
The zero-based index of the element to retrieve.

defValue

Default value to return.

Return Value

defValue if the index is outside the bounds of the source sequence; otherwise, the element at the specified position in the source sequence.


Example

var names = [ "Hartono, Tommy", "Adams, Terry", "Andersen, Henriette Thaulow",  "Hedlund, Magnus", "Ito, Shu" ];

var index = 20;
var name = from(names).atOrDefault(index, null);

console.log("The name chosen at index " + index + " is '" +
    (name == null ? "[no name at this index]" : name) + "'.");

/*
 This code produces the following output:

 The name chosen at index 20 is '[no name at this index]'.
*/ 
Clone this wiki locally