Professional Documents
Culture Documents
<body>
<script language="javascript">
document.write("This is a java script program output"); //
javascript code
</script>
</body>
</html>
II. Blocks
<script type="text/javascript">
var a=10;
for(i=1; i<=10; i++)
{
document.write(i);
}
</script>
<script type="text/javascript">
document.write(myfunc(12));
document.write(myfunc(12));
document.write(myfunc(12));
</script>
</body>
</html>
VIII. Flow Control
• Conditional control
– if control
– if…else control
– switch control
• Loop control
– for control
– while control
– break and continue
IF control
• If Statement
• Syntax
• if (condition) { code to be executed if
condition is true }
IF… ELSE control
• If...else Statement
• If you want to execute some code if a
condition is true and another code if
the condition is not true, use the
if....else statement.
• Syntax
• if (condition) { code to be executed if
condition is true } else { code to be
executed if condition is not true }
If...else if...else control
• If...else if...else Statement
• You should use the if....else if...else
statement if you want to select one of
many sets of lines to execute.
• Syntax
• if (condition1) { code to be executed if
condition1 is true } else if (condition2)
{ code to be executed if condition2 is
true } else { code to be executed if
condition1 and condition2 are not
Switch control
• The JavaScript Switch Statement
• You should use the switch statement
if you want to select one of many
blocks of code to be executed.
• Syntax
• switch(n) { case 1: execute code
block 1 break; case 2: execute code
block 2 break; default: code to be
executed if n is different from case 1
and 2 }
For loop control
• The for loop is used when the number
of iterations is known.
• Syntax
for (var=startvalue;var<=endvalue;var=var+increment)
{
code to be executed
}
while loop control
• The while loop
• The while loop is used when you
want the loop to execute and
continue executing while the
specified condition is true.
while (var<=endvalue)
{
code to be executed
}
Transfer statements
• JavaScript break and continue
Statements
• There are two special statements
that can be used inside loops:
– break and continue.
• JavaScript For...In Statement
• The for...in statement is used to loop
(iterate) through the elements of an
array or through the properties of an
object.
• The code in the body of the for ... in
loop is executed once for each
element/property.
• Syntax
• for (variable in object) { code to be
executed } The variable argument can
be a named variable, an array element,
or a property of an object.
IX. Objects
• to group together methods and properties
into one data structure.
• doesn't support inheritance or
polymorphism
• encapsulation is simply the process of
wrapping up those functions and variables
into a package.
• variables are known as properties and
functions are known as methods
• method is just a function that is contained
inside of an object
• Property is simply a variable that is
Objects
• Built-In Objects
• math.sqrt()
• User-Defined Objects
• Properties inside the object is set using this
statement
• Functions are used to create objects
e.g., function myfn(arg1,arg2,arg2)
{ this.arg1=arg1,
this.arg2=arg2;
this.arg3=arg3;
}
User-Defined Objects(contd.)
• This cookie-cutter is referred to as the
object’s constructor.
• Using this cookie-cutter to create an object
is known as instantiation.
• myobj=new myfn(“abc”,12,12.22)
• Object properties are referred as
a1=myobj.arg1;
a2=myobj.arg2;
a3=myobj.arg3;
• You can achieve the same with ‘with’
statement
with(myobj)
{ a1=arg1;
X. Arrays
• Allows you to hold an arbitrary number of
“things” without defining beforehand what
they will hold.
• An array is created much like an object,
using Array as the constructor, with the
number of elements as the argument for
that constructor.
• e.g., list = new Array(10);
I/O and PopUp Boxes
• Alert Box
• An alert box is often used if you want to
make sure information comes through to the
user.
• Syntax: alert(“text");
• Confirm Box
• A confirm box is often used if you want the
user to accept something.
• If the user clicks "OK", the box returns true.
If the user clicks "Cancel", the box returns
false.
• Syntax: confirm(“text");
I/O and PopUp Boxes
• Prompt Box
• if you want the user to input a value
• When a prompt box pops up, the user will
have to click either "OK" or "Cancel" to
proceed after entering an input value.
• If the user clicks "Cancel" the box returns
null.
• Syntax:
prompt(“inputtext","defaultvalue");
String Object