Tuesday, 19 January 2021

JavaScript Array

 What is Array:

- array is a single variable that is used to store different elements.

- by default index of array is 0

- Array is stored multiple values inside a Single Variable Using New Keyword

____________________________________


Difference Between Array and Variable

- Array is Stored Multiple Values inside a Single variable using New Keyword

- Variable is Stored only Single value At a time 

 Example of Variable and Array:

<script>

//variable is store run time only one value at a time

a = 1,2,3,4;

document.write(a);

document.write("<hr>");


        //array

a = new Array(10,20,30,40,100);

document.write(a);

</script>

Output of variable:

1

Output of Array:

1,2,3,4

_______________________________________________

Index wise Array Printing:

<script>

//index wise array fetch

a = new Array(10,30,40,50,100)

document.write(a);

document.write("<hr>");

document.write(a['1']);

document.write("<hr>");

document.write(a['4']);

</script>

Output:

10,30,40,50,100

_____________

30

_____________

100

______________________________________________________

Array With ForLoop:

<script>

jay = new Array("jayesh","kishan","nayan",30,40);

document.write(jay[0]);

document.write("<hr>");

for(i=0;i<=4;i++)

{

alert(jay[i]);

document.write("Your data is:"+jay[i]+"<br>");

}

</script>



No comments:

Post a Comment