SIGN UP MEMBER LOGIN:    
ARTICLE

Introducing JavaScript

Posted by Vineet Kumar Saini Articles | ASP, JavaScript, CSS November 02, 2011
This article shows the Fundamentals of JavaScript. In this article you can easily understand looping and functions in JavaScript.
Reader Level:
Download Files:
 

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>

Output

image2.gif

If-else program in JavaScript.

<
html>
<
body>
<
script type="text/javascript">
    var d = new Date()
    var time = d.getHours()
    if (time < 10) {
        document.write("<b>Good morning</b>")
    }
    else {
        document.write("<b>Good day</b>")
    }
</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>

Output

ifelse.gif

Switch case in JavaScript.

<
html>
<
body>
<
script type="text/javascript">
    var d = new Date()
    theDay = d.getDay()
    switch (theDay) {
        case 5:
            document.write("<b>Finally Friday</b>")
            break
        case 6:
            document.write("<b>Super Saturday</b>")
            break
        case 0:
            document.write("<b>Sleepy Sunday</b>")
            break
        default:
            document.write("<b>I'm really looking forward to this weekend!</b>")
    }
</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>

Output

switch.gif

For loop in JavaScript.

<html>
<
body>
<
script type="text/javascript">
    for (i = 0; i <= 5; i++) {
        document.write("The number is " + i)
        document.write("<br />")
    }
</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>

Output

for.gif

While loop in JavaScript.

<html>
<body>
<script type="text/javascript">
    var i = 0;
    while(i <= 5){
        document.write("The number is " + i)
        document.write("<br />")
        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>

Output

while.gif

Do-while loop in JavaScript.

<html>
<<body>
<script type="text/javascript">
    var i = 0;
    do {
        i++;
        document.write("The number is " + i)
        document.write("<br />")
    }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>

Output

do.gif

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("<br />");
    }
</script>
</
body>
</
html>

Output

break.gif

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("<br />");
    }
</script>
</
body>
</
html>

Output

conti.gif

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>

Output

function.gif

Login to add your contents and source code to this article
share this article :
post comment
 

Thanks...

Posted by Vineet Kumar Saini Nov 18, 2011

:) Tx

Posted by Raanan Nov 18, 2011
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Team Foundation Server Hosting
Become a Sponsor