Before starting the Java Script Tutorial, Let us see the how add the JAVA SCRIPT in the HTML document../
In the HTML document we can write the java script in the HEAD tag and also in the BODY tag, using SCRIPT tag.
Syntax of writing java script in HTML document...
Under Head tag
<HEAD>
<SCRIPT LANGUAGE = "JavaScript">
OR
<SCRIPT TYPE="text/JavaScript">
---------------------------------------
---------------------------------------
BODY OF THE JAVASCRIPT
---------------------------------------
---------------------------------------
</SCRIPT>
</HEAD>
OR
Under Body tag
<BODY>
<SCRIPT LANGUAGE = "JavaScript">
OR
<SCRIPT TYPE="text/JavaScript">
---------------------------------------;
---------------------------------------;
BODY OF THE JAVASCRIPT;
---------------------------------------;
---------------------------------------;
</SCRIPT>
</BODY>
NOW, Lets go to the JavaScript Tutorial.........
JAVA SCRIPT IS CASE SENSITIVE LANGUAGE.
Commenting in JavaScript
// Single line comment
/*Multiline
comment*/
Local JavaScript Variables
A variable declared within a JavaScript function becomes LOCAL and can only be accessed within that function. (the variable has local scope).
You can have local variables with the same name in different functions, because local variables are only recognized by the function in which they are declared.
Local variables are deleted as soon as the function is completed.
Global JavaScript Variables
Variables declared outside a function become GLOBAL, and all scripts and functions on the web page can access it.
Global variables are deleted when you close the page.
Syntax for IF Statement:
if(condition)
{
-----------;
-----------;
Statements;
-----------;
-----------;
}
Syntax for IF-ELSE statement:
Syntax for Switch Statement:
switch(myVar) //myVar value must be an integer or a character.
{
case 1:
-----------;
-----------;
Statements;
-----------;
-----------;
break;
case 2:
-----------;
-----------;
Statements;
-----------;
-----------;
break;
case 3:
| | |
| | |
| | |
default:
-----------;
-----------;
Statements;
-----------;
-----------;
break;
}
3. For SWITCH statement:
switch(myVar) //myVar value must be an integer or a character.
{
case 1: //If myVar is 1 this is executed
-----------;
-----------;
Statements;
-----------;
-----------;
break;
case 2: //If myVar is 2 this is executed
-----------;
-----------;
Statements;
-----------;
-----------;
break;
default: //If myVar is does not satisfy any case
-----------;
-----------;
Statements;
-----------;
-----------;
break;
}
//Break is used here for getting out from the switch statement.
var i=0;
while(i<=5)
{
alert(i); // Shows the 6 alert windows with the value of "i"...
i++;
}
BREAK STATEMENT:
The break statement is used for terminating the current WHILE of FOR loop and then transferring program control to the statement just after the terminated loop.
Example:
for(var i=0; i<=5; i++)
{
alert(i);
if(i == 3) //Here, if the i is 3 then the loop is terminated from for loop.
{
break;
}
}
//This is show only 4 alert windows with the value of "i" on your browser
CONTINUE STATEMENT:
A continue statement terminate execution of the block of statements in a WHILE or FOR loop and continues execution of the loop with the next iteration.
Example:
for(var i=0; i<=5; i++)
{
if(i == 3 || i == 4) //Here, if the i is 3 or 4 then the loop is terminated for the next iteration.
{
continue;
}
alert(i);
}
//This is shows alert windows with the value of "i" except whenever "i" is 3 or 4 alert windows.
NEXT
To Be Continued.................
Please leave the comments... and give feedbacks...
In the HTML document we can write the java script in the HEAD tag and also in the BODY tag, using SCRIPT tag.
Syntax of writing java script in HTML document...
Under Head tag
<HEAD>
<SCRIPT LANGUAGE = "JavaScript">
OR
<SCRIPT TYPE="text/JavaScript">
---------------------------------------
---------------------------------------
BODY OF THE JAVASCRIPT
---------------------------------------
---------------------------------------
</SCRIPT>
</HEAD>
OR
Under Body tag
<BODY>
<SCRIPT LANGUAGE = "JavaScript">
OR
<SCRIPT TYPE="text/JavaScript">
---------------------------------------;
---------------------------------------;
BODY OF THE JAVASCRIPT;
---------------------------------------;
---------------------------------------;
</SCRIPT>
</BODY>
NOW, Lets go to the JavaScript Tutorial.........
JAVA SCRIPT IS CASE SENSITIVE LANGUAGE.
Commenting in JavaScript
// Single line comment
/*Multiline
comment*/
Every Statement must be terminated by Semicolon( ; )
VARIABLES:
Variables store and retrieve data/values. A variable can refer to a value, which changes or is changed. For declaring a variable there are some rules are as follows:
1. Variable should be start with the letter, Underscore"_", Dollar($).
2. Variable should be unique in his own scope(about scope we can read later).
3. Variable must not contain any blank space and special characters except underscore"_".
4. Subsequent digits can also be digits (0-9).
We can declare JavaScript variables with the var keyword:
Example:
var a; //single variable declaration.
var _a;
var $a;
var a90, a_bc, $r; // Multiple variables declaration.
Assigning value to variables:
var a = 90;
OR
var a;
a = 90;
Now Scope of variable:
A variable declared within a JavaScript function becomes LOCAL and can only be accessed within that function. (the variable has local scope).
You can have local variables with the same name in different functions, because local variables are only recognized by the function in which they are declared.
Local variables are deleted as soon as the function is completed.
Global JavaScript Variables
Variables declared outside a function become GLOBAL, and all scripts and functions on the web page can access it.
Global variables are deleted when you close the page.
DATA TYPES:
Java Script considers data to fall into several possible types. Depending on the type of data certain operations may or may not be allowed on the values. Variables can be of these types:
1. NUMBER :-> Integer(can be expressed in Decimal, Hexadecimal(leading with 0x/0X), Octal(leading with 0)) and Floating-point numbers
2. BOOLEAN :-> True or False
3. STRING :-> Strings are delineated by single(') or double(") quotation marks.
STATEMENTS AND OPERATORS:
OPERATORS | FUNCTIONALITY |
---|---|
= | Assigning the value |
+= | Shortcut for Adding to the current value |
-= | Shortcut for Subtracting from the current value |
*= | Shortcut for Multiplying the current value |
/= | Shortcut for Dividing the current value |
OPERATORS | FUNCTIONALITY |
---|---|
== | Returns true value if the items are same |
!= | Returns true value if the items are not same |
> | Returns true if the item on the left is greater than the item on the right |
>= | Returns true if the item on the left is equal to or greater than the item on the right |
< | Returns true if the item on the left is less than the item on the right |
<= | Returns true if the item on the left is equal to or less than the item on the right |
OPERATORS | FUNCTIONALITY |
---|---|
+ | Add two values together |
- | Subtract one value from another |
* | Multiply two values |
/ | Divides the value on the left by the one on the right and returns the result |
++X | Increments the value, then return the value |
X++ | Returns the value, and then increments the value |
--X | Decrements the value, then return the value |
X-- | Returns the value, and then decrements the value |
OPERATORS | FUNCTIONALITY |
---|---|
&& | Looks at two expressions and returns a value of TRUE if the expressions on the left and right of the operators are both true |
|| | Looks at two expressions and returns a value of TRUE if either one --but not both-- of the expressions are true |
CONTROL STRUCTURES:
JavaScript supports the usual control structures:
1. The Conditional :-> If, If-else, and switch.
2. The Iterations/Loop :-> For, While, Do-While, Break, Continue.
CONDITIONAL STATEMENTS:
When we want to perform different actions for different decisions, then we used Conditional Statements.
In JavaScript we have three Conditional Statements:
1. IF :-> we use this statement if we want to execute a set of code when a condition is true.
2. IF-ELSE :-> we use this statement if we want to select one of two sets of code to execute.
3. SWITCH :-> we use this statement if we want to select one of many sets of code to execute.
Syntax for IF Statement:
if(condition)
{
-----------;
-----------;
Statements;
-----------;
-----------;
}
Syntax for IF-ELSE statement:
if(condition) { -----------; -----------; Statements; -----------; -----------; } else { if(condition) { -----------; -----------; Statements; -----------; -----------; } else { -----------; -----------; Statements; -----------; -----------; } } |
if(condition) { -----------; -----------; Statements; -----------; -----------; } else if(condition) { -----------; -----------; Statements; -----------; -----------; } |
if(condition) { -----------; -----------; Statements; -----------; -----------; } else if(condition) { -----------; -----------; Statements; -----------; -----------; } else { -----------; -----------; Statements; -----------; -----------; } |
Syntax for Switch Statement:
switch(myVar) //myVar value must be an integer or a character.
{
case 1:
-----------;
-----------;
Statements;
-----------;
-----------;
break;
case 2:
-----------;
-----------;
Statements;
-----------;
-----------;
break;
case 3:
| | |
| | |
| | |
default:
-----------;
-----------;
Statements;
-----------;
-----------;
break;
}
Examples:
1. For IF statement:
var a=10;
if(a==10)
{
b="true";
}
else
{
b="false";
}
2. For IF-ELSE statement:
var a = 10; if(a==10) { b="true"; } else { if(a == 5) { b="Other value"; } else { b = "false"; } } |
var a = 10; if(a==10) { b="true"; } else if(a == 5) { b="Other value"; } else { b = "false"; } |
3. For SWITCH statement:
switch(myVar) //myVar value must be an integer or a character.
{
case 1: //If myVar is 1 this is executed
-----------;
-----------;
Statements;
-----------;
-----------;
break;
case 2: //If myVar is 2 this is executed
-----------;
-----------;
Statements;
-----------;
-----------;
break;
default: //If myVar is does not satisfy any case
-----------;
-----------;
Statements;
-----------;
-----------;
break;
}
//Break is used here for getting out from the switch statement.
LOOP/ITERATION STATEMENTS:
A loop is a set of commands that executes repeatedly until a specified condition is met/TRUE. Java script supports two loop statements: FOR and WHILE.
FOR LOOP:
A for loop repeats until a specified condition evaluates to false.
Syntax for FOR loop:
for(initial-expression; condition; increment/decrement-expression)
{
- - - - - - - - - - - - - -;
- - - - - - - - - - - - - -;
Statements;
- - - - - - - - - - - - - -;
- - - - - - - - - - - - - -;
}
Example:
for(var i=0; i<=5; i++)
{
alert(i); // Shows the 6 alert windows with the value of "i"...
}
Example:
for(var i=0; i<=5; i++)
{
alert(i); // Shows the 6 alert windows with the value of "i"...
}
WHILE LOOP:
The while statement defines a loop that iterates as long as a condition remains true.
Syntax for WHILE loop:
initial-expression;
while(condition)
{
- - - - - - - - - - - - - -;
- - - - - - - - - - - - - -;
Statements;
- - - - - - - - - - - - - -;
- - - - - - - - - - - - - -;
increment/decrement-expression;
}increment/decrement-expression;
Example:
var i=0;
while(i<=5)
{
alert(i); // Shows the 6 alert windows with the value of "i"...
i++;
}
BREAK STATEMENT:
The break statement is used for terminating the current WHILE of FOR loop and then transferring program control to the statement just after the terminated loop.
Example:
for(var i=0; i<=5; i++)
{
alert(i);
if(i == 3) //Here, if the i is 3 then the loop is terminated from for loop.
{
break;
}
}
//This is show only 4 alert windows with the value of "i" on your browser
CONTINUE STATEMENT:
A continue statement terminate execution of the block of statements in a WHILE or FOR loop and continues execution of the loop with the next iteration.
Example:
for(var i=0; i<=5; i++)
{
if(i == 3 || i == 4) //Here, if the i is 3 or 4 then the loop is terminated for the next iteration.
{
continue;
}
alert(i);
}
//This is shows alert windows with the value of "i" except whenever "i" is 3 or 4 alert windows.
NEXT
To Be Continued.................
Please leave the comments... and give feedbacks...
No comments:
Post a Comment