The document discusses JavaScript arrays. It explains that arrays can store multiple values in a single variable. It provides examples of how to create arrays, add and access elements, modify values, and loop through arrays. It also mentions various array methods like sort(), concat(), and join() and provides a link to more complete documentation on all JavaScript array properties and methods.
Download as DOC, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
25 views
26 Arary Object
The document discusses JavaScript arrays. It explains that arrays can store multiple values in a single variable. It provides examples of how to create arrays, add and access elements, modify values, and loop through arrays. It also mentions various array methods like sort(), concat(), and join() and provides a link to more complete documentation on all JavaScript array properties and methods.
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3
JavaScript Array Object
The Array object is used to store multiple values in a single variable.
Try it Yourself - Eamples !reate an array !reate an array" assign values to it" and #rite the values to the output. $or...%n Statement &o# to use a for...in statement to loop through the elements of an array. Join t#o arrays - concat'( &o# to use the concat'( method to join t#o arrays. )ut array elements into a string - join'( &o# to use the join'( method to put all the elements of an array into a string. *iteral array - sort'( &o# to use the sort'( method to sort a literal array. +umeric array - sort'( &o# to use the sort'( method to sort a numeric array. !omplete Array Object ,eference $or a complete reference of all the properties and methods that can be used #ith the Array object" go to our complete Array object reference. The reference contains a brief description and eamples of use for each property and method- .hat is an Array/ An array is a special variable" #hich can hold more than one value" at a time. %f you have a list of items 'a list of car names" for eample(" storing the cars in single variables could loo0 li0e this1 cars1="Saab"; cars2="Volvo"; cars3="BMW"; &o#ever" #hat if you #ant to loop through the cars and find a specific one/ And #hat if you had not 2 cars" but 233/ The best solution here is to use an array- An array can hold all your variable values under a single name. And you can access the values by referring to the array name. Each element in the array has its o#n %4 so that it can be easily accessed. !reate an Array The follo#ing code creates an Array object called my!ars1 var myCars=new Array(); There are t#o #ays of adding values to an array 'you can add as many values as you need to define as many variables you re5uire(. 61 var myCars=new Array(); myCars[0="Saab"; myCars[1="Volvo"; myCars[2="BMW"; You could also pass an integer argument to control the array7s si8e1 var myCars=new Array(3); myCars[0="Saab"; myCars[1="Volvo"; myCars[2="BMW"; 91 var myCars=new Array("Saab"!"Volvo"!"BMW"); Note: %f you specify numbers or true:false values inside the array then the type of variables #ill be numeric or ;oolean instead of string. Access an Array You can refer to a particular element in an array by referring to the name of the array and the inde number. The inde number starts at 3. The follo#ing code line1 "oc#men$%wr&$e(myCars[0); #ill result in the follo#ing output1 Saab <odify =alues in an Array To modify a value in an eisting array" just add a ne# value to the array #ith a specified inde number1 myCars[0="'(el"; +o#" the follo#ing code line1 "oc#men$%wr&$e(myCars[0); #ill result in the follo#ing output1 '(el