Course Hive
Search

Welcome

Sign in or create your account

Continue with Google
or
Arrays in javascript
Play lesson

JavaScript Tutorial - Arrays in javascript

4.0 (0)
13 learners

What you'll learn

This course includes

  • 12.3 hours of video
  • Certificate of completion
  • Access on mobile and TV

Summary

Keywords

Full Transcript

Link for all dot net and sql server video tutorial playlists http://www.youtube.com/user/kudvenkat/playlists Link for slides, code samples and text version of the video http://csharp-video-tutorials.blogspot.com/2014/12/arrays-in-javascript.html Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our YouTube channel. Hope you can help. https://www.youtube.com/channel/UC7sEwIXM_YfAMyonQCrGfWA/?sub_confirmation=1 Arrays are collections and ZERO indexed. This means the first element is at index ZERO and the last element is at index arrayObject.length - 1. length property of the array object returns the size of the array. The following JavaScript code creates an empty array. length property returns 0 in this case. var emptyArray = []; alert(emptyArray.length); Output : 0 Another way to create an array is by busing the Array constructor as shown below. In this example we are setting the length of the array to 10. var myArray = new Array(10); alert(myArray.length); Output : 10 Retrieving first and last elements from the array using the array index var myArray = [10, 20, 30]; document.write("First element = " + myArray[0] + "[br/]"); document.write("Last element = " + myArray[myArray.length - 1] + "[br/]"); Output : First element = 10 Last element = 30 Populating an array in JavaScript : There are several ways to populate an array in JavaScript. Let's look at those different option now. Declaring an array first and then populating using the array index var myArray = []; myArray[0] = 10; myArray[1] = 20; myArray[2] = 30; alert(myArray); Output : 10, 20, 30 Declaring and populating the array at the same time var myArray = [10, 20, 30]; alert(myArray); Output : 10, 20, 30 Declaring an array first using the Array constructor and then populating using the array index. Though the initial size is set to 3, adding a fourth element to the array will not throw an exception, because arrays in JavaScript can grow in size. var myArray = new Array(3); myArray[0] = 10; myArray[1] = 20; myArray[2] = 30; alert(myArray); Output : 10, 20, 30 Declaring and populating the array at the same time using the Array constructor var myArray = new Array(10, 20, 30); alert(myArray); Output : 10, 20, 30 Please note : If only one number is passed to the Array constructor, then that number is used to set the size of the array. If more that one number is passed then those will be used as elements to populate the array. A for loop can be used to populate and retrieve elements from the array object in JavaScript Populating an array using for loop : The following JavaScript code stores even numbers in the array from 0 to 10. Notice that we are using a for loop to populate the array. var evenNumbersArray = []; for (var i = 0; i [= 5; i++) { evenNumbersArray[i] = i * 2; } alert(evenNumbersArray); Output : 0,2,4,6,8,10 Retrieving elements from the array using for loop : var evenNumbersArray = []; for (var i = 0; i [= 5; i++) { evenNumbersArray[i] = i * 2; } for (var i = 0; i [ evenNumbersArray.length; i++) { document.write(evenNumbersArray[i] + "[br/]"); } Output : 0 2 4 6 8 10

Course Hive

Continue this lesson in the app

Install CourseHive on Android or iOS to keep learning while you move.

Related Courses

FAQs

Course Hive
Download CourseHive
Keep learning anywhere