Example : A JS program in Array to find the length of an array.
<html>
	<head>
		<h4>Array Examples</H4>
	</head>
	<body>
		<script>			
		   var vehicle = new Array( "car", "bus", "truck","tractor" );
		   document.write("The length of array is = " + vehicle.length); 					
		</script>
	</body>
</html>

Output:
Array Examples

The length of array is = 4
Example : A JS program in Array to display the first and last elements of an array.
<html>
	<head>
		<h4>Array Examples</H4>
	</head>
	<body>
		<script>			
			var value = new Array(10,20,30,40,50);
			document.write(value[0]," ", value[4]);
                        //document.write(value[0] + " " + value[4]);
		</script>
	</body>
</html>

Output:
Array Examples

10 50
Example : A JS program in Array to display the elements of an array.
<html>
	<head>
		<h4>Array Examples</H4>
	</head>
	<body>
		<script>			
			var value = new Array(100,200,300,400,500);			
			document.write(value);			
		</script>
	</body>
</html>

Output:
Array Examples

100,200,300,400,500

------------------  OR  -------------------

<html>
	<head>
		<h4>Array Examples</H4>
	</head>
	<body>
		<script>			
			var vehicle = new Array("car", "bus", "truck","tractor");
			document.write(vehicle); 					
		</script>
	</body>
</html>

Output:
car,bus,truck,tractor

------------------  OR  -------------------

<html>
	<head>
		<h4>Array Examples</H4>
	</head>
	<body>
		<script>			
			var vehicle = new Array( "car", "bus", "truck","tractor" );
			for(var i=0; i<4;i++)
			{
				document.write(vehicle[i]+" ");				
			}							
		</script>
	</body>
</html>

Output:
Array Examples

car bus truck tractor

------------------  OR  -------------------

<html>
	<head>
		<h4>Array Examples</H4>
	</head>
	<body>
		<script>			
			var vehicle = new Array( "car", "bus", "truck","tractor" );
			for(var i=0; i<vehicle.length;i++)
			{
			   document.write(vehicle[i]+" ");				
			}							
		</script>
	</body>
</html>

Output:
Array Examples

car bus truck tractor

------------------  OR  -------------------

<html>
	<head>
		<h4>Array Examples</H4>
	</head>
	<body>
		<script>			
			var vehicle = new Array( "car", "bus", "truck","tractor" );
			for(var i=0; i<vehicle.length;i++)
			{				
			   document.write(vehicle[i]+"</br>");
			}							
		</script>
	</body>
</html>

Output:
Array Examples

car
bus
truck
tractor
Example : A JS program in Array to display the alphanumeric elements of an array.
<html>
	<head>
		<h4>Array Examples</H4>
	</head>
	<body>
		<script>			
			var value = new Array(10,"car","30",'p',50);
			for(var i=0; i<value.length;i++)
			{				
			   document.write(value[i]+"</br>");
			}							
		</script>
	</body>
</html>
Example : A JS program in Array accepts user input values from the user at run time and displays all the elements of the array.
<html>
   <body>
        <script>			
            var value = new Array();
            var y= prompt("Enter the value for array size to store elements");			
            
            for(var i=0; i<y;i++)
            {
            var x=prompt("Enter 10 values","Enter "+(i+1)+" numeric value for array");				
            value[i]=x;
            }

            document.write("The " + value.length + " outputs are = ");
            for(var i=0; i<y;i++)
            {
            document.write(value[i]+" ");			
            }
        </script>
    </body>
</html>

------------------------  OR  ------------------------

<html>
	<head>
		<h4>Array Examples</H4>
	</head>
	<body>
		<script>			
			var value = [];
			var y= prompt("Enter the array size to store elements");			
			
			for(var i=0; i<y;i++)
			{					
			 value[i]=prompt("Enter " +(i+1)+" numeric value for array");		
			}

			document.write("The outputs are = ");			
            for(i=0; i<y;i++)
			{
			 document.write(value[i]+" ");			
			}
            
		</script>
	</body>
</html>

Output:
Array Examples

The outputs are = 10 20 30 40 50
Example : A JS program in Array to combine/merge two or more arrays as one.
<html>
	<head>
		<h4>Array Examples</H4>
	</head>
	<body>
		<script>			
			var fruit = new Array("Mango","Apple","Guava");
			var flower = new Array("Rose","Marigold","Jasmine");			
			var combine=fruit.concat(flower);
			document.write(combine);
		</script>
	</body>
</html>

Output: 
Mango,Apple,Guava,Rose,Marigold,Jasmine
Example : A JS program in Array to display the components in reverse order using the reverse() method.
<html>
	<head>
		<h4>Array Examples</H4>
	</head>
	<body>
		<script>			
			var fruit = new Array("Mango","Apple","Guava");
			document.write(fruit.reverse());
		</script>
	</body>
</html>

Output:
Guava,Apple,Mango
Example : A JS program in Array to display the first element of an array using the shift() method.
<html>
	<head>
		<h4>Array Examples</H4>
	</head>
	<body>
		<script>			
			var fruit = new Array("Mango","Apple","Guava");
			document.write(fruit.shift());
		</script>
	</body>
</html>

Output:
Mango

NB: shift() returns first single value from the array.
Example : A JS program in Array to add the element at first place in an array using the unshift() method.
<html>
	<head>
		<h4>Array Examples</H4>
	</head>
	<body>
		<script>			
			var fruit = new Array("Mango","Apple","Guava");
			fruit.unshift("Orange");
			document.write(fruit);
			//document.write(fruit.sort());
		</script>
	</body>
</html>

Output:

Orange,Mango,Apple,Guava
Example : A JS program in Array displays all the elements in an ascending sorted order using the sort() method.
<html>
	<head>
		<h4>Array Examples</H4>
	</head>
	<body>
		<script>			
			var fruit = new Array("Mango","Apple","Guava");
			document.write(fruit.sort());
		</script>
	</body>
</html>

Output:
Apple,Guava,Mango
Example : A JS program to create and display all the elements of Two Dimensional Array.
<html>
	<head>
		<h4>Array Examples</H4>
	</head>
	<body>
		<script>			
			let tda = [
    					['Lily', 20, 60, 'A'],
    					['Romy', 10, 52, 'B'],
    					['Robert', 5, 24, 'F']    
			          ];			

			document.write("The outputs are = ");			
                        document.write(tda[0][0]); 
			document.write(" ");
			document.write(tda[0][3]);
			document.write(" ");
			document.write(tda[2][0]);
			document.write(" ");
			document.write(tda[2][2]);            
		</script>
	</body>
</html>

--------------------  OR  --------------------

<html>
	<head>
		<h4>Array Examples</H4>
	</head>
	<body>
		<script>			
			var tda = [
    					["Lily", 20, 60, "A"],
    					["Romy", 10, 52, "B"],
    					["Robert", 5, 24, "F"]    
				  ];			

			document.write("The outputs are = ");
			
                        document.write(tda[0][0]); 
			document.write(" ");
			document.write(tda[0][3]);
			document.write(" ");
			document.write(tda[2][0]);
			document.write(" ");
			document.write(tda[2][2]);            
		</script>
       </body>
</html>

Output:
Array Examples

The outputs are = Lily A Robert 24

Loading


0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.