dprogrammer Posted February 14, 2013 Share Posted February 14, 2013 Welcome to the course for Javascript. Although it is aimed at complete beginners to the subject I do assume that you have some knowledge of HTML and a little CSS. You don’t have to be an expert, though. Just the basics will do. You don’t need to buy any software for this Javascript course - you probably already have everything you need to get started.A normalJavascript variable likevar age = 18 holds a single pieceof data. An array is a way to holdmore than one piece of dataunder the same name. There’s afew ways to set up an array butthe simplest looks like this:var my_array = [10, “ten”, true];This sets up an array to holdthree values all under the singlevariable name we calledmy_array. Notice that you canstore numbers, strings of text,and Boolean values in an array.Each value needs to be separatedby a comma. The values need togo between two square bracketsand after an equal sign.To get at the values in your arrayyou use something called theindex value. This is a numberbetween square brackets. Thenumber corresponds to a value’sposition in the array (positionnumbers start at 0):my_array[0]my_array[1]my_array[2]So my_array[0] will hold the value10, my_array[1] will hold the text"ten", and my_array[2] will holdthe Boolean value true. You couldthen write the values to a webpage:document.write( my_array[0] +"");document.write( my_array[1] +"");document.write( my_array[2] +"");Test it out with the followingscript:Run the script in a browser andyou should see the number 10displayed. Now change the 0 inbetween the square brackets ofmy_array[0] to a 1. Save thechange and refresh. The text"ten" will be written out. Changethe 1 into a 2 and you’ll see"true" displayed.Now change the 2 into a 3. Thistime the word “undefined” willdisplay. The reason is becauseyou only set up your array tohold 3 items. When you typed a 3instead of a 2 you were trying toaccess item number 4 (0 to 3,remember). But you haven’tdefined a fourth item. Eventhough you haven’t got a fourthitem, Javascript doesn’t throwyou up an error, it doesn’t refuseto work. Instead, it adds a newposition to the end of your array.You can even store something inthis new position. Change yourscript to this (the new parts arein bold):var my_array = [10, “ten”, true];document.write( my_array[3] +"");my_array[3] =“new item”;document.write( my_array[3] );Javascript first prints out"undefined" then “new item”.Notice the way we’re assigningthis new value:my_array[3] =“new item”;On the left of the equal sign, wehave the name of the array thena pair of square brackets. Insideof the square brackets is thearray position number we wantto access. After the equal signyou type the value you want toassign. So it’s just like normalvariable assignment except youadd the square brackets and thearray position after the variablename.Another way to set up an array iswith the new keyword:var my_array = new Array( );This time after the equal sign wehave the word new then a space.The word Array comes next(uppercase A and neverlowercase). Immediately after theword Array you need a pair ofround brackets. If you like youcan add how many positions thearray is going to hold:var my_array = new Array(3);This says “set up an array with 3positions”. You don’t have to addany array positions, though. It’sup to you.However, when you’re assigningvalues to each position you doneed to add the index positions.You do it like this:var my_array = new Array( );my_array[0] = 10;my_array[1] = “ten”;my_array[2] = true;So the first line sets up an arraycalled my_array. The next threelines assign values to eachposition in the new array. Themethod above is the one we’lluse to set up an array from nowon.Arrays and LoopsArrays are quite often used withloops. That’s because the indexposition of arrays are numbersthat you swap for the loopvariable. As an example, study thefollowing code (of course, youcan try it out as well):var counter = 0;var lottery_numbers = newArray();while ( counter < 49 ) {lottery_numbers[counter] =counter + 1;document.write( lottery_numbers[counter] +"" );counter++;}What the script does is to assignvalues to an array calledlottery_numbers. We’ve used awhile loop. The loop goes roundand round executing this line:lottery_numbers[counter] =counter + 1;Instead of sayinglottery_numbers[0],lottery_numbers[1],lottery_numbers[2], etc, we’vereplaced the index number withthe variable name counter. Wecan do this because counterchanges each time round theloop. Javascript knows what’s inthe counter variable and usesthis as the index. After the equalsign, we’re assigning whatever isin the counter variable (plus 1) toeach position in the array. (Canyou see why we’d need to add1?)There’s a lot more you can dowith arrays, and you’ll meet themagain in a later section. For now,let’s move on. Next up isfunctions. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.