I have the following code:
function lastElement(array) {
if (array.length > 0)
return array[array.length - 1];
else
return undefined;
}
alert(lastElement([ undefined,1, 2]));
Which returns 2 since it doesn't counts the undefined element & that's perfectly fine.Now if i swap the last element in the array with the undefined:
function lastElement(array) {
if (array.length > 0)
return array[array.length - 1];
else
return undefined;
}
alert(lastElement([ 1, 2,undefined]));
It returns undefined!! Why is it so? Any help appreciated.