Web Programming TutorialsImplementing Array and Variable in Java Script.

Implementing Array and Variable in Java Script.

In this session today we will learn to implement variable and array in java script.

  • To implement arrays and variables in java script follow the steps given below :

  1. Create an new folder on desktop.
  2. Inside that folder create an file index.html
  3. Now right click on file index.html and select an option edit with notepad++.
  4. v0

  5. After it gets opened in Notepad++ write the following code in it.
    <!DOCTYPE html>
    <html>
    <head>
        <title>Variable and Array</title>
    	
    	<script>
    		var car='Honda';
            </script>
    </head>
    	<body>
                  <script>
    		document.write(car);
                  </script>
    
           </body>
    </html>
    
  6. The notepad++ window will look like as shown below :
  7. v1

  8. And the output of the window is shown below:
  9. v2

  10. Now write the following code in an Notepad++ window.
    <!DOCTYPE html>
    <html>
    <head>
        <title>Variable and Array</title>
    	
    	<script>
    		 x=5;
    		 y=5;
            </script>
    </head>
    	<body>
                 <script>
    		     document.write(x+y);
                 </script>
    
            </body>
    </html>
    
  11. Your window will have the following look :
  12. v3

  13. The output of the following window is as follows :
  14. v4

  15. We can also concatenate the string ,so for concatenating the string write the code given below in single quote.
    <!DOCTYPE html>
    <html>
    <head>
        <title>Variable and Array</title>
    	
    	<script>
    		 x='5';
    		 y='5';
            </script>
    </head>
    	<body>
        <script>
    		document.write(x+y);
        </script>
    
    </body>
    </html>
    
  16. Your coded Notepad++ window will look like as shown below :
  17. v5

  18. So you will have the following output window:
  19. v6

  20. Let’s take another example for declaring a variable,so write the following code in Notepad++ window.
    <!DOCTYPE html>
    <html>
    <head>
        <title>Variable and Array</title>
    	
    	<script>
    		 x='5';
    		 
        </script> 
    </head>
    	<body>
        <script>
    		document.write('I HAVE '  +x+  ' APPLE ');
        </script>
    
    </body>
    </html>
    
  21. So after writing the code your Notepad++ window will look like as shown below :
  22. v7

  23. After running it on browser you will have following Output :
  24. v8

  25. Now let’s learn to display the array element,so for displaying it write the following code in Notepad++.
    <!DOCTYPE html>
    <html>
    <head>
        
    
    	<script>
    		
    		var mycars = new Array('Honda','Toyoto','Fort');
    		
    	</script>
        
    <body>
    
        <script>
                     document.write(mycars[0]);
        </script> 
    
    </body>
    </html>
    
  26. Your coded window will have following look :
  27. v9

  28. You will have following output :
  29. v10

  30. We can also add an next element in an array and access it by calling its index value,so to do it lets write the following code :
    <!DOCTYPE html>
    <html>
    <head>
        
    
    	<script>
    		
    		var mycars = new Array('Honda','Toyoto','Fort');
    		mycars[3]='Maruti'
    	</script>
        
    <body>
    
        <script>
                     document.write(mycars[3]);
        </script> 
    
    </body>
    </html>
    
  31. The Notepad++ window will look like this:
  32. v11

  33. You will have the following output :
  34. v12

  35. We can also declare an array using square bracket.
    <!DOCTYPE html>
    <html>
    <head>
        
    
    	<script>
    		
    		var mycars = ['Honda','Toyoto','Fort'];
    		mycars[3]='Maruti'
    	</script>
        
    <body>
    
        <script>
                     document.write(mycars[2]);
        </script> 
    
    </body>
    </html>
    
  36. v13

  37. You will have following output :
  38. v14

  39. An array is an object and Object has the properties,so to understand it write the following code :
    <!DOCTYPE html>
    <html>
    <head>
        
    
    	<script>
    		
    		var mycars = ['Honda','Toyoto','Fort'];
    		mycars[3]='Maruti'
    	</script>
        
    <body>
    
        <script>
                     document.write(mycars.length);
        </script> 
    
    </body>
    </html>
    
  40. Your coded window will have the following look :
  41. v15

  42. You will have the following output :
  43. v16

  44. Thus we have learnt to implement arrays and variables in Java Script.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Exclusive content

- Advertisement -

Latest article

21,501FansLike
4,106FollowersFollow
106,000SubscribersSubscribe

More article

- Advertisement -