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 :
- Create an new folder on desktop.
- Inside that folder create an file index.html
- Now right click on file index.html and select an option edit with notepad++.
- 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>
- The notepad++ window will look like as shown below :
- And the output of the window is shown below:
- 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>
- Your window will have the following look :
- The output of the following window is as follows :
- 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>
- Your coded Notepad++ window will look like as shown below :
- So you will have the following output window:
- 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>
- So after writing the code your Notepad++ window will look like as shown below :
- After running it on browser you will have following Output :
- 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>
- Your coded window will have following look :
- You will have following output :
- 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>
- The Notepad++ window will look like this:
- You will have the following output :
- 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>
- You will have following output :
- 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>
- Your coded window will have the following look :
- You will have the following output :
- Thus we have learnt to implement arrays and variables in Java Script.