Reader Level:
Articles

Introducing JavaScript

By Vineet Kumar Saini on November 02, 2011
This article shows the Fundamentals of JavaScript. In this article you can easily understand looping and functions in JavaScript.
  • 0
  • 0
  • 13954
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

Vineet Kumar Saini

Vineet Kumar Saini is MCA qualified with Honors from SunderDeep Engineering College Ghaziabad. Currently he is working as a "WEB Developer" at AzylApps Technology Pvt. Ltd.

COMMENT USING

Trending up