JavaScript Array

 In JavaScript, an array is an ordered list of values. Each value is called an element specified by an index.

Every value is associated with numeric index starting with 0. The following figure illustrates how an array stores values.



Array Initialization:-

-- array can be initialized by three ways:-

1) Using array literal
2)  Creating instance of object directly(new keyword)
3) Using array constructor

1)JavaScript array literal :-

 
Syntax:-

  1. var arrayname=[value1,value2.....valueN];  

Example:-

<html>
<body>
<script>  
var techcode=["html","css","javaScript"];  
for (i=0;i<techcode.length;i++){  
document.write(techcode[i] + "<br/>");  
}  
</script>  
</body>
</html>

Output:-

html
css
javaScript

2)JavaScript Array directly (new keyword):-

Syntax:-

var arrayname=new Array();  

Example:-

<html>
<body>
<script>  
var i;  
var techcode= new Array();  
techcode[0] = "html";  
techcode[1] = "css";  
techcode[2] = "javaScript";  
  
for (i=0;i<techcode.length;i++){  
document.write(techcode[i] + "<br>");  
}  
</script>  
</body>
</html>


Output:-

html
css
javaScript

3)JavaScript array constructor:-

Syntax:-

var arrayname=new Array(value 1, value 2....value[N]);  

Example:-

<html>
<body>
<script>  
var techcode=new Array("html","css","javaScript");  
for (i=0;i<techcode.length;i++){  
document.write(techcode[i] + "<br>");  
</script>  
</body>
</html>

Output:-

html
css
javaScript

Post a Comment

0 Comments