Professional Documents
Culture Documents
Unit Iii Java Script: Variables, Operators, Functions, Control Structures, Events, Objects Introduction To Java Script
Unit Iii Java Script: Variables, Operators, Functions, Control Structures, Events, Objects Introduction To Java Script
Unit Iii Java Script: Variables, Operators, Functions, Control Structures, Events, Objects Introduction To Java Script
<script type=”text/javascript”>
…………………..
</script>
2. Identifiers:
Identifiers are the names given to variables. These variables hold the
data value.
Some of the rules for handling identifiers in java script:
• Identifiers must start with either letter or underscore or dollar sign.
• There is no limit on the length of identifiers,
• The letters of Identifiers are case sensitive.
• Programmer defined variable names must not have upper case letters.
3. Reserved Words:
4. Comments:
5. Semicolon:
While writing java script , the web programmer must give semicolon at
the end of statements.
Using an Internal JavaScript:
External.html
<html>
<head>
<title>External javascripts</title>
</head>
<body>
<p>Java Scripts</p>
< script type=”text/javascript” src=”myscript.js”> </script>
< /body>
</html
myscript.js
document.write(“Welcome to JavaScript Language”);
Variables:
• Variable names are case sensitive (y and Y are two different variables)
• Variable names must begin with a letter, the $ character, or the underscore
character.
• Try to use descriptive names for variables. This makes the code easier to
understand.
Primitive types: The following are the primitive types in java script.
1. Number
2. String
3. Boolean
4. Undefined
5. Null
Numeric:
These are called as numbers. Because they can store the number
values.
These numbers include integer, floating and double values.
Ex: 10, 5.789
String:
Note:
When assigning a text value to a variable, put quotes around the value.
When assigning a numeric value to a variable, do not put quotes
around the value, if you put quotes around a numeric value, it will be
treated as text.
Boolean:
Undefined:
Null:
The null values can be assigned by using the reserved word null .
The null means no value.
If we try to access the null value then a runtime error will occurs.
Declaring (Creating ) JavaScript Variables:
Ex:
<html>
<head>
<title>Variable declaration</title>
</head>
<body>
<h2>Variable declaration</h2>
<script type="text/javascript">
var a=10,b=15,c;
c=a+b;
document.write("After addition ,the result is:");
document.write(c);
</script>
</body>
</html>
1. Local variables:
2. Global variables:
3. Auto declaration:
4. Collision:
Operators:
o Arithmetic operators
o Assignment operators
o Comparison operators
o Logical operators
o String operators
Arithmetic operators:
Assignment operators:
Given that x=10 and y=5, the table below explains the assignment
operators:
Operator Example Same Result
As
= x=y x=5
Comparison operators:
Given that x=5, the table below explains the comparison operators:
Operator Description Comparing Returns
x==5 true
Logical operators:
Logical operators are used to determine the logic between variables or values.
Given that x=6 and y=3, the table below explains the logical operators:
EX: <html>
<head>
<title>String Operators</title>
</head>
<body>
<h2>String Operators</h2>
<script type="text/javascript">
var str1="Welcome";
var str2="MLWEC";
document.write ("<h3>"+str1+"To"+str2+"</h3>");
</script>
</body>
</html>
Conditional Operator:
Syntax:
Variable name=(condition)?value1:value2
Function functionname()
{
some code
}
Example:
<html>
<head>
<script type="text/javascript">
function myFunction()
{
alert("Hello World!");
}
</script>
</head>
<body>
<button onclick="myFunction()">Click me</button>
</body>
</html>
Calling a Function with Arguments:
• When calling a function, we can pass along some values to it, these
values are called arguments or parameters.
• These arguments can be used inside the function.
• We can send as many arguments separated by commas (,).
Syntax: myFunction(argument1,argument2)
function myFunction(var1,var2)
{
some code
}
Example:
<html>
<body>
<p>Click the button to call a function with arguments</p>
<script type="text/javascript">
function myFunction(name,job)
{
alert("Welcome " + name + ", the " + job);
}
</script> </body> </html>
if-Statement:
Syntax:
if (condition)
{
code to be executed if condition is true
}
Example:
<html>
<head> <title>if statement</title> </head>
<body>
<h2>if statement</h2>
<script type="text/javascript">
date=new Date();
time=date.getHours();
if(time<12)
document.write("<h3>"+"Gudmorning"+"</h3>");
</script>
</body>
</html>
if-else Statement:
if-else Statement
Use the if....else if...else statement to select one of
several blocks of code to be executed.
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 neither condition1 nor condition2 is
true
}
switch Statement:
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
}
Example:
<html>
<body>
<p>Click the button to display what day it is today.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script type="text/javascript">
function myFunction()
{
var x;
var d=new Date().getDay();
switch (d)
{
case 0: x="Today it's Sunday"; break;
case 1: x="Today it's Monday"; break;
case 2: x="Today it's Tuesday"; break;
case 3: x="Today it's Wednesday"; break;
case 4: x="Today it's Thursday"; break;
case 5: x="Today it's Friday"; break;
case 6: x="Today it's Saturday"; break;
}
document.getElementById("demo").innerHTML=x;
}
</script>
</body>
</html>
Looping:
Objects:
• Methods in Math object are used for manipulation of numbers and to perform
any common mathematical calculations.
• It contains many rounding methods like floor value, ceil value, round value
and many trigonometric functions like sin, cos and tan functions and other
functions like max, min etc…
<html>
<head>
<title>math functions</title>
</head>
<body>
<center>
<script type="text/javascript">
document.writeln(Math.abs(40));
document.writeln(Math.ceil(40.8));
document.writeln(Math.floor(40.8));
document.writeln(Math.round(40.0004));
document.writeln(Math.max(3,4));
document.writeln(Math.min(78,40));
document.writeln(Math.sqrt(400));
document.writeln(Math.log(4));
document.writeln(Math.exp(4));
</script>
</center>
</body>
</html>
String Object:
Example:
<html>
<head>
<title>String functions</title>
</head>
<body>
<center>
<script type="text/javascript">
var s1=”hai”, s2=”hello”;
document.writeln(“First string is:”+s1);
document.writeln(“Second string is:”+s2);
document.writeln(“length of fst stringis:”+s1.length());
document.writeln(s1.toUpperCase());
document.writeln(s2.toLowerCase());
document.writeln(“cocatnatingstrings:”+s1.concat(s2));
</script>
</center> </body> </html>
Date Object:
Meaning
Method
getDate() Returns 1 to 31,day of month
getDay() Returns 0 to 6 ,Sunday to Saturday
getMonth() Returns 0 to 11
getFullYear() Returns four digit year number
getHours() Returns 0 to 23
getMinutes() Returns 0 to 59
getSeconds() Returns 0 to 59
getTime() Rweturns number of millisec from
jan 1970
Set Methods:
Meaning
Method
setDate(v) To Set Date, day, month, full year
setDay(v)
setMonth(v)
setFullYear(y,m,d)
setHours() To Set
setMinutes() hours,minutes,seconds,milliseconds,time
setSeconds()
setTime()
setMilliseconds()
<html>
<body>
<script type="text/javascript">
var d=new Date();
document.write(d);
</script>
</body> </html>
<html>
<head>
<script type="text/javascript">
function startTime()
{
var today=new Date();
var h=today.getHours();
var m=today.getMinutes();
var s=today.getSeconds();
// add a zero in front of numbers<10
m=checkTime(m);
s=checkTime(s);
document.getElementById('txt').innerHTML=h+":"+m+":"+s;
t=setTimeout(function(){startTime()},500);
}
function checkTime(i)
{
if (i<10)
{
i="0" + i;
}
return i;
}
</script>
</head>
<body onload="startTime()">
<div id="txt"></div> </body> </html>
Boolean Object:
Sample Program:
<html>
<body>
<script type="text/javascript">
var d=new Boolean(false);
document.write(“<b>”+”The Boolean value is:”);
document.write(temp.toString());
</script>
</body>
</html>
Number Object:
Property Meaning
MAX_VALUE Largest possible number get
displayed
MIN_VALUE Smallest possible number
NaN When not a number,NaN
displyed
Events:
Examples of events:
Example:
<html>
<head>
<title>onload events</title>
<script type="text/javascript">
function sample()
{
alert("welcome");
}
</script>
</head>
<body onload="sample()">
</body> </html>