Saturday, January 5, 2013

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 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 any 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:

Simple program of JavaScript.

<html>
<
head>
<
title>MY TITLE</title>
</head>
<
body>
<
script type="text/javascript">
    document.write("Most Welcome in JavaScript")
 
</script>
</
body>
</
html>



If-else program in JavaScript.

<
html>
<
body>
<
script type="text/javascript">
    var d = new Date()
    var time = d.getHours()
    if (time < 10) {
        document.write("Good morning")
    }
    else {
        document.write("Good day")
    }
</script>
<
p>
This example demonstrates the If...Else statement.
</p>
<
p>
If the time on your browser is less than 10,
you will get a "Good morning" greeting.
Otherwise you will get a "Good day" greeting.
</p>
</
html>


Switch case in JavaScript.

<
html>
<
body>
<
script type="text/javascript">
    var d = new Date()
    theDay = d.getDay()
    switch (theDay) {
        case 5:
            document.write("Finally Friday")
            break
        case 6:
            document.write("Super Saturday")
            break
        case 0:
            document.write("Sleepy Sunday")
            break
        default:
            document.write("I'm really looking forward to this weekend!")
    }
</script>
<
p>This JavaScript will generate a different greeting based on what day it is. Note that Sunday=0, Monday=1, Tuesday=2, etc.</p>
</body>
</
html>

For loop in JavaScript.
<html>
<
body>
<
script type="text/javascript">
    for (i = 0; i <= 5; i++) {
        document.write("The number is " + i)
        document.write("
"
)
    }
</script>
<
p>Explanation:</p>
<p>This for loop starts with i=0.</p>
<p>As long as <b>i</b> is less than, or equal to 5, the loop will continue to run.</p>
<p><b>i</b> will increase by 1 each time the loop runs.</p>
</body>
</
html>


While loop in JavaScript.
<html>
<body>
<script type="text/javascript">
    var i = 0;
    while(i <= 5){
        document.write("The number is " + i)
        document.write("
"
)
        i++
    }
</script>
<
p>Explanation:</p>
<p>This while loop starts with i=0.</p>
<p>As long as <b>i</b> is less than, or equal to 5, the loop will continue to run.</p>
<p><b>i</b> will increase by 1 each time the loop runs.</p>
</body>
</
html>


Do-while loop in JavaScript.
<html>
<<body>
<script type="text/javascript">
    var i = 0;
    do {
        i++;
        document.write("The number is " + i)
        document.write("
"
)
    }while(i<=5)
</script>
<
p>Explanation:</p>
<p>This do-while loop starts with i=0.</p>
<p>As long as <b>i</b> is less than, or equal to 5, the loop will continue to run.</p>
<p><b>i</b> will increase by 1 each time the loop runs.</p>
</body>
</
html>


Break statement in JavaScript.
<html>
<
body>
<
script type="text/javascript">
    var i = 0;
    for (i = 0; i <= 10; i++) {
        if (i == 3) {
            break;
        }
        document.write("The number is " + i);
        document.write("
"
);
    }
</script>
</
body>
</
html>



Continue statement in JavaScript.
<html>
<
body>
<
script type="text/javascript">
    var i = 0
    for (i = 0; i <= 10; i++) {
        if (i == 3) {
            continue;
        }
        document.write("The number is " + i);
        document.write("
"
);
    }
</script>
</
body>
</
html>


Function in JavaScript.
<html>
<
head>
<
script type="text/javascript">
    function product(a, b) {
        return a * b
    }
</script>
</
head>
<
body>
<
script type="text/javascript">
    document.write(product(4, 3))
</script>
<
p>The script in the body section calls a function with two parameters (4 and 3).</p>
<p>The function will return the product of these two parameters.</p>
</body>
</
html>











 


No comments:

Post a Comment