Introducing JavaScript

Introduction

 
JavaScript is a kind of scripting language. Scripting Languages are used to put logic with HTML tags. They are small languages, having no environment. It is executed in the browser with HTML. 
 
All the scripting languages are Interpreted languages. There are some script languages, which are as follows:
  1. java Script:- Netscape 
  2. Jscript :- MS (C/c++/java/c#)
  3. Vbscript :- MS (vb)

Why we use JavaScript?

 
It is mainly used for Client-side validations.
 

Fundamentals Of JavaScript

  1. There are no specific data type (e.g. :-var t) in JavaScript.
  2. There are if, for, switch, while, do-while, break, continue same as java or c# in JavaScript.
  3. document.write() is used for display output in JavaScript.
  4. There are some dialogues in JavaScript, which are as follows
     
    Alert -- Ok
    Confirm -- Ok/CANCEL
    Prompt -- Input

  5. Function :-There are 'function' keyword used for function in JavaScript.
Some Examples of JavaScript:
 
A simple program of JavaScript.
  1. <html>  
  2. <head>  
  3. <title>MY TITLE</title>  
  4. </head>  
  5. <body>  
  6. <script type="text/javascript">  
  7.     document.write("Most Welcome in JavaScript")  
  8.  </script>  
  9. </body>  
  10. </html> 
Output
 
image2.gif
 
If-else program in JavaScript
  1. <html>  
  2. <body>  
  3. <script type="text/javascript">  
  4.     var d = new Date()  
  5.     var time = d.getHours()  
  6.     if (time < 10) {  
  7.         document.write("<b>Good morning</b>")  
  8.     }  
  9.     else {  
  10.         document.write("<b>Good day</b>")  
  11.     }  
  12. </script>  
  13. <p>  
  14. This example demonstrates the If...Else statement.  
  15. </p>  
  16. <p>  
  17. If the time on your browser is less than 10,  
  18. you will get a "Good morning" greeting.  
  19. Otherwise you will get a "Good day" greeting.  
  20. </p>  
  21. </html> 
Output
 
ifelse.gif
 
Switch case in JavaScript
  1. <html>  
  2. <body>  
  3. <script type="text/javascript">  
  4.     var d = new Date()  
  5.     theDay = d.getDay()  
  6.     switch (theDay) {  
  7.         case 5:  
  8.             document.write("<b>Finally Friday</b>")  
  9.             break  
  10.         case 6:  
  11.             document.write("<b>Super Saturday</b>")  
  12.             break  
  13.         case 0:  
  14.             document.write("<b>Sleepy Sunday</b>")  
  15.             break  
  16.         default:  
  17.             document.write("<b>I'm really looking forward to this weekend!</b>")  
  18.     }  
  19. </script>  
  20. <p>This JavaScript will generate a different greeting based on what day it is. Note that Sunday=0, Monday=1, Tuesday=2, etc.</p>  
  21. </body>  
  22. </html> 
Output
 
switch.gif
 
For loop in JavaScript
  1. <html>  
  2. <body>  
  3. <script type="text/javascript">  
  4.     for (i = 0; i <= 5; i++) {  
  5.         document.write("The number is " + i)  
  6.         document.write("<br />")  
  7.     }  
  8. </script>  
  9. <p>Explanation:</p>  
  10. <p>This for loop starts with i=0.</p>  
  11. <p>As long as <b>i</b> is less than, or equal to 5, the loop will continue to run.</p>  
  12. <p><b>i</b> will increase by 1 each time the loop runs.</p>  
  13. </body>  
  14. </html> 
Output
 
for.gif
 
While loop in JavaScript
  1. <html>  
  2. <body>  
  3. <script type="text/javascript">  
  4.     var i = 0;  
  5.     while(i <= 5){  
  6.         document.write("The number is " + i)  
  7.         document.write("<br />")  
  8.         i++  
  9.     }  
  10. </script>  
  11. <p>Explanation:</p>  
  12. <p>This while loop starts with i=0.</p>  
  13. <p>As long as <b>i</b> is less than, or equal to 5, the loop will continue to run.</p>  
  14. <p><b>i</b> will increase by 1 each time the loop runs.</p>  
  15. </body>  
  16. </html> 
Output
 
while.gif
 
Do-while loop in JavaScript 
  1. <html>  
  2. <<body>  
  3. <script type="text/javascript">  
  4.     var i = 0;  
  5.     do {  
  6.         i++;  
  7.         document.write("The number is " + i)  
  8.         document.write("<br />")  
  9.     }while(i<=5)  
  10. </script>  
  11. <p>Explanation:</p>   
  12. <p>This do-while loop starts with i=0.</p>  
  13. <p>As long as <b>i</b> is less than, or equal to 5, the loop will continue to run.</p>  
  14. <p><b>i</b> will increase by 1 each time the loop runs.</p>  
  15. </body>  
  16. </html> 
Output
 
do.gif
 
Break statement in JavaScript
  1. <html>  
  2. <body>  
  3. <script type="text/javascript">  
  4.     var i = 0;  
  5.     for (i = 0; i <= 10; i++) {  
  6.         if (i == 3) {  
  7.             break;  
  8.         }  
  9.         document.write("The number is " + i);  
  10.         document.write("<br />");  
  11.     }  
  12. </script>  
  13. </body>  
  14. </html> 
Output
 
break.gif
 
Continue statement in JavaScript
  1. <html>  
  2. <body>  
  3. <script type="text/javascript">  
  4.     var i = 0  
  5.     for (i = 0; i <= 10; i++) {  
  6.         if (i == 3) {  
  7.             continue;  
  8.         }  
  9.         document.write("The number is " + i);  
  10.         document.write("<br />");  
  11.     }  
  12. </script>  
  13. </body>  
  14. </html> 
Output
 
conti.gif
 
Function in JavaScript
  1. <html>  
  2. <head>  
  3. <script type="text/javascript">  
  4.     function product(a, b) {  
  5.         return a * b  
  6.     }  
  7. </script>  
  8. </head>  
  9. <body>  
  10. <script type="text/javascript">  
  11.     document.write(product(4, 3))  
  12. </script>  
  13. <p>The script in the body section calls a function with two parameters (4 and 3).</p>  
  14. <p>The function will return the product of these two parameters.</p>  
  15. </body>  
  16. </html> 
Output
 
function.gif


Similar Articles