Chapter 1
Basics of JavaScript Programming
�Overview of JavaScript: �
�Features of JavaScript: �
�Features of JavaScript cont.: �
�Enabling JavaScript for various Browsers: �
�Enabling JavaScript for various Browsers: �
�Enabling JavaScript for various Browsers: �
�Writing a JavaScript Document : �
------------
-------------
</script>
Two attributes of <script> tag are:
</script>
</script>
�Writing a First Program: �
<html>
<head></head>
<body>
<script type="text/javascript">
document.write("HELLO WORLD");
</script>
<p>this is web page body</p>
</body>
</html>
�Explanation of Program: �
<script type=“=“text/javascript”>
----------
</script>
//
/* */ or can use XHTML comment
<! -- > and < -->
Open any web browser and type complete name of file on the
address bar.
�Object Properties: �
�Values: �
It uses six types of values:
�Keywords: �
abstract else instanceof super
boolean enum int switch
break export interface synchronized
byte extends let this
case false long throw
catch final native throws
char finally new transient
class float null true
const for package try
continue function private typeof
debugger goto protected var
default if public void
delete implements return volatile
do import short while
double in static with
�Operators and Expressions: �
Sr. No. | Type | Operator | Meaning | Example |
1. | Arithmetic | + | Addition or Unary plus | c=a+b |
- | Subtraction or Unary minus | d=-a | ||
* | Multiplication | c=a*b | ||
/ | Division | c=a/b | ||
% | Mod | c=a%b | ||
2. | Logical | && | AND Operator | 0&&1 |
|| | OR Operator | 0 | | 1 | ||
3. | Increment | ++ | Increment by one | ++i or i++ |
4. | Decrement | -- | Decrement by one | --i or i-- |
5. | Conditional | ? : | Conditional Operator | a>b?true:false |
�Operators and Expressions cont.: �
Sr. No. | Type | Operator | Meaning | Example |
6. | Assignment | = | Is assigned to | a=5 |
+= | Add a value then assign | a+=5 (a=a+5) | ||
-= | Subtract a value then assign | a-=5(a=a-5) | ||
*= | Multiply a value then assign | a*=5(a=a*5) | ||
/= | Divide a value then assign | a/=5(a=a/5) | ||
7. | Relational | < | Less than | a<4 |
> | Greater than | a>4 | ||
<= | Less than equal to | a<=4 | ||
>= | Greater than equal to | a>=4 | ||
== | Equal to | a==4 | ||
!= | Not Equal to | a!=4 | ||
=== | equal value and equal type | a===5 | ||
!== | not equal value or not equal type | a!==5 |
�AWT Classes: �
AWT Classes form three catagories:
1) Graphics: Color, Font, Images, Polygons, etc.
2) Components: GUI Components: Button, Menus, Panel, Frame
Container class contains Components, LayoutManagers
3)LayoutManager: Gives rules for graphical layout of components in a container.
Decision Making and Branching
Decision Making and Branching Cont.
if (Condition) OR if (Condition)
{ statement(s); statement;
}
Condition contains Relational operators as well as Logical Operators.
Ex: if (i > 0)
{
- - - - - - - ;
}
if (i > 1 || j!=5)
{
- - - - - - - ;
}
Decision Making and Branching Cont.
2) if else Statement
if (condition)
{
stmt(s) for true case;
}
else
{
stmt(s) for true case;
}
Or
if (condition)
stmt for true case;
else
stmt for true case;
Multiple Alternative if Statements
if (score >= 90)
grade = ‘A’;
else
if (score >= 80)
grade = ‘B’;
else
if (score >= 70)
grade = ‘C’;
else
if (score >= 60)
grade = ‘D’;
else
grade = ‘F’;
switch Statements
switch (variable-name)
{
case value1:
Execute stmt1;
break;
case value2:
Execute stmt2;
break;
case valueN:
Execute stmtN;
break;
default:
Execute last;
}
Decision Making and Looping
1) While Loop
while (condition is true)
{
// loop-body;
}
e.g.
int a = 20;
while(a>10)
{
document.write(“JPR”);
a--;
}
Decision Making and Looping
2) do while Loop
do
{
// Loop body;
}
while (condition is true);
e.g.
int a = 0;
do
{
document.write(a);
a++;
}while(a<50);
Decision Making and Looping
3) for Loop
for(initialization; condition; incr/decrement)
{
// Loop body;
}
e.g.
int a;
for(a=0;a<10;a++)
{
document.write(a);
}
OR
int a;
for(a=0;a<10;a++)
document.write(a);
Decision Making and Looping
4) for in Loop
for (var in object)
{
code block to be executed
}
Parameter Description
var Required. A variable that iterates over properties of an object
object Required. The specified object that will be iterated
�break Statement�
Syntax:
jump-statement;
break;
Ex:
for (i = 0; i < 10; i++)
{
if (i === 3)
{ break; }
text += "The number is " + i + "<br>";
}
�break Statement�
Output:
The number is 0
The number is 1
The number is 2
�continue Statement�
jump-statement;
break;
Ex:
for (i = 0; i < 10; i++)
{
if (i ==3)
{ continue; }
text += "The number is " + i + "<br>";
}
�continue Statement�
Output:
The number is 0
The number is 1
The number is 2
The number is 4
The number is 5
The number is 6
The number is 7
The number is 8
The number is 9