You are on page 1of 23

Java Script

Operators :
• Arithmetic
– + - * / %
• Comparison
– == != < > <= >=
• Logical
– && || !
• Bitwise
– & | ^ >> >>> <<
Ternary
– ?:
Variables :
• Types are not specified.
• Initial values are optional.
• Rules for JavaScript variable names:
– Variable names are case sensitive (y and Y are two
different variables)
– Variable names must begin with a letter or the
underscore character
Eg :
– var name;
– var nrErrors = 0;
– var a, b, c;
Example :
<html>
<body>
<script language="javascript">
var x;
x=5+5;
document.write(x);
document.write("<br />");
x="5"+"5";
document.write(x);
document.write("<br />");
x=5+"5";
document.write(x);
document.write("<br />");
x="5"+5;
document.write(x);
document.write("<br />");
</script>
<p>The rule is: If you add a number and a
string, the result will be a string.</p>
</body>
</html>
Data Types
Data Types

Primitive Complex

1. Number
Eg: 22, 3.5, 2E3, -44.5, 0x5F 1. Array
2. Boolean 2. Object
Eg: True(1), False(0)
3. String
Eg: “India”, ‘M.Sc(IT)’
4. Null
Conditional Statements :
• If – use to execute some code only if a
specified condition is true.
Syntax:
if(condition1)
{ True statements }
else if(condition 2)
{ True statements }
else
{ True statements }
Example :
<script language="javascript">
var per=30;
if (per<35)
{document.write("<b>fail</b>"); }
else if (per>=35 && per<50)
{ document.write("<b>Pass class</b>"); }
else if (per>=50 && per<60)
{ document.write("<b>Second Class</b>"); }
else if (per>=60 && per<70)
{ document.write("First Class"); }
else
{ document.write("Distinction"); }
</script>
Conditional Statements :
• Switch – use to select one of many blocks of code
to be executed.
Syntax:
switch(var_name)
{ case 1:
execute code block1
break;
case 2:
execute code block 2
break;
default:
code to be executed if n is different
from case 1 and 2
}
Example :
Conditional Statements :
• Loops - for loop, while loop
for loop – loops through block of code a specified
no of times.

Syntax:
for(var=startvalue;var<=endvalue;var=var+increment)
{
code to be executed
}
Example :
<html>
<body bgcolor="pink">
<script language="javascript">
var i=0;
for(i=0;i<=5;i++)
{
document.write(“<b>No is:<b>"+i+"<br>");
}
</script>
</body>
</html>
Conditional Statements :
while loop – loops through block of code whilea specified condition is
true.
Syntax: while (var<=endvalue)
{ code to be executed }

Example :
<html> <body>
<script language=“javascript”>
var i=0;
while (i<=5)
{ document.write("The number is " + i);
document.write("<br />");
i++;
}
</script>
</body></html>
Conditional Statements :
do…while loop – execute block of code ONCE &
then it will repeat the loop as long as the specified
condition is true.

Syntax:
do
{
code to be executed
}while (var<=endvalue);
Example :
<html>
<body>
<script language="javascript">
var i=0;
do
{
document.write("The number is " + i);
document.write("<br />");
i++;
}while (i<=5);
</script>
</body>
</html>
break Statements :
will break the loop & continue executing the code
that follows after the loop if any.
Example :
<html><body>
<script language="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>
continue Statements :
will break the current loop & continue with the next
value.
Example :
<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>
Popup Boxes :
Three types
• Alert box
• Confirm box
• Prompt box
Alert box
• used if you want to make sure information
comes through to the user.
• the user will have to click "OK" to proceed.
Syntax :
alert("sometext");
Example :
<html> <body>
<script language=javascript”>
alert(“Alert box”);
</script>
<body></html>
Confirm box:

• Used if you want the user to verify or accept something.


• It will pops up with ‘OK’ and ‘CANCEL’ button.
• When user clicks OK, the box will return TRUE & if
user clicks CANCEL, the box will return FALSE.

Syntax :
confirm("sometext");
Example :
<html>
<body>
<script language="javascript">
var r=confirm("Press a button");
if (r==true)
{ alert("You pressed OK!"); }
else
{ alert("You pressed Cancel!"); }
</script>
</body>
</html>
Prompt box :
• used when you wants to take input value from the
user.
• If user clicks OK button than it will returns input
value & if user clicks CANCEL button than it will
returns null.

Syntax :
prompt("sometext","defaultvalue");
Example :
<html>
<body>
<script language="javascript">
var name=prompt("Please enter your name",”ABC");
if (name!=null && name!="")
{
document.write(“Welcome" + name);
}
</script>
</body
</html>

You might also like