Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

9. Array() > Length versus Index

Length versus Index

An array starts indexing values at 0. This means that the first numeric slot to hold a value in an array looks like myArray[0]. This can be a bit confusing—if I create an array with a single value, the index of the value is 0 while the length of the array is 1. Make sure you understand that the length of an array represents the number of values contained within the array, while the numeric index of the array starts at zero.

Below, the string value blue is contained in the myArray array at the numeric index 0, but since the array contains one value, the length of the array is 1.

Live Code

<!DOCTYPE html><html lang="en"><body><script>

var myArray = ['blue'] // the index 0 contains the string value 'blue'
console.log(myArray[0]); // logs 'blue'
console.log(myArray.length); // logs 1

</script></body></html>

  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint