Undefined when trying to get the upper bound of array?
I was just thinking back to the good old days when I used to use VBScript (good old days?!?!? What am I thinking?), and I remember using two very useful开发者_开发技巧 methods with arrays, these were UBound and LBound. Now I don't think JavaScript has these and if I wanted to make my own I could extend the object however, I just tried doing this to return the upperbound item (or the last item of the array)
var myArray = ['1','2','3','4','5','6','seven', 8, 'nine','10'];
document.write(myArray.length + ' ' + myArray[myArray.length]);
the myArray.length returns what it should however when I try and output the last item using myArray[myArray.length] I get undefined? Anyone know why this is?
Thanks
The last element of myArray is at myArray.length - 1, since arrays in Javascript are 0-indexed. So , myArray[myArray.length -1] is what you're looking for.
The indexes start at 0. The last element is at myArray.length - 1
加载中,请稍侯......
精彩评论