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() > Defining Arrays with a Predefined Length

Defining Arrays with a Predefined Length

As I mentioned earlier, by passing a single integer parameter to the Array() constructor, it’s possible to predefine the array’s length, or the number of values it will contain. In this case, the constructor makes an exception and assumes you want to set the length of the array and not pre-populate the array with values.

Below, we set up the myArray array with a predefined length of 3. Again, we are configuring the length of the array, not passing it a value to be stored at the 0 index.

Live Code

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

var myArray = new Array(3);console.log(myArray.length); /* logs 3, because we are passing one 
                             numeric parameter */
console.log(myArray[0]); // logs undefined

</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 10-Day Trial


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