Archive Pages Design$type=blogging

How to Loop Through JavaScript Array?

You can loop any Array by using different types of looping statements in JavaScript , which I have previously posted. In this post you can l...

You can loop any Array by using different types of looping statements in JavaScript, which I have previously posted. In this post you can loop through JavaScript Array. At first let you know basic concepts of Array ie. defining array, adding values to the array and accessing array elements. 

The Array object is used to store a set of values in a single variable name. You can define Array object with the new keyword.

The following line of code defines an Array object called  myArray.

var myArray=new Array()

You can also define the array size by passing an integer argument as follows.

var myArray=new Array(5)

You can add the values to an array as the following.

var myArray=new Array()
myArray[0]="value1"
myArray[1]="value2"
myArray[2]="value3"


You can also give values to the arrays while defining it as below.

var myArray=new Array("value1", "value2", "value3")

While accessing Arrays, you can refer to a particular element in an array by referring to the name of the array and the index number. The index number starts at 0. The following line accesses the first value of the array myArray.

document.write(myArray[0])


Loop Through JavaScript Array


You can loop any Array by using any of the following looping statements in JavaScript.

Using For loop


You can loop any Array by using For Loop as below.

var myArray=new Array()
myArray[0]="value1"
myArray[1]="value2"
myArray[2]="value3"

for(i=0; i<myArray.length;i++)
{
document.write(myArray[i])
}

Example:


<html>
<head></head>
<body>
<script type="text/javascript">
var mycars=new Array()
mycars[0]="Saab"
mycars[1]="Volvo"
mycars[2]="BMW"

for(i=0; i<mycars.length;i++)
{
document.write(mycars[i]+"<br/>")
}
</script>

Preview:


Using For .... In Statement


You can loop any Array by using For .... In Statement as below.

var myArray=new Array()
myArray[0]="value1"
myArray[1]="value2"
myArray[2]="value3"

for(x in myArray)
{
document.write(myArray[x])
}

Example:


<html>
<head></head>
<body>
<script type="text/javascript">
var mycars=new Array()
mycars[0]="Saab"
mycars[1]="Volvo"
mycars[2]="BMW"

for(x in mycars)
{
document.write(mycars[x]+"<br/>")
}
</script>
</body>
</html>

Preview:


You can also loop any Array by using other looping statements like while and Do ... while loops as the same methods given above.

Read Next:How to Concatenate, Join and Sort Array in JavaScript?

Related Search Terms

تعليقات

الاسم

Artificial Intelligence(AI) Backlinking Basic IT Best List Blogging Tips C C# C++ Computer Architecture Computer Fundamental Computer Security Computer/IT Officer Exam CSS Data Mining and Warehousing Data Recovery Tools Data Structure Database Management System E-commerce E-government Internet & Web Designing IT Law IT Tips and Tricks IT Tutorials Java JavaScript Keyword Research Tools MIS Multiple Choice Question (MCQ) Networking Online Earning Online IT Jobs Operating System Oracle Forms and Reports Programming Guide Programming Language SEO Social Networking Sites Software Download Software Engineering System Analysis and Design Top List VB.Net
false
rtl
item
Max5: How to Loop Through JavaScript Array?
How to Loop Through JavaScript Array?
Max5
https://maxs27.blogspot.com/2015/02/how-to-loop-through-javascript-array.html
https://maxs27.blogspot.com/
http://maxs27.blogspot.com/
http://maxs27.blogspot.com/2015/02/how-to-loop-through-javascript-array.html
true
1760660989564394152
UTF-8
لم يتم العثور على اى مواضيع شاهد الكل اقرأ المزيد رد احذف الرد حذف بواسطة الرئيسية صفحات مواضيع شاهد الكل نوصى لك التسميات الارشيف بحث لم يتم العثور على ماتبحث عنه عودة للرئيسية الاحد الاثنين الثلاثاء الاربعاء الخميس الجمعة السبت الاحد الاثنين الثلاثاء الاربعاء الخميس الجمعة السبت يناير فبراير مارس ابريل مايو يونيو يوليو اغسطس سبتمبر اكتوبر نوفمبر ديسمبر يناير فبراير مارس ابريل مايو يونيو يوليو اغسطس سبتمبر اكتوبر نوفمبر ديسمبر just now 1 دقيقة مضت $$1$$ دقيقة مضت 1 hour ago $$1$$ ساعة مضت الامس $$1$$ يوم مضى $$1$$ اسبوع مضى اكثر من 5 اسابيع مضت