SlideShare a Scribd company logo
JAVA SCRIPT
REASON FOR USING JAVA SCRIPT
 HTML is suitable for developing web pages
 Web pages that are developed using html are STATIC
web pages
 That is, in html web pages we can display and format
text, insert links and images etc.,,
 But these web pages will have the same look every time
it is executed.
 There is no interaction with the web page
REASON FOR USING JAVA SCRIPT (CONTD…..)
 In order to increase this interaction we must integrate
these web pages with some programming techniques.
 To add this interactivity to HTML web pages JAVA SCRIPT
is used
INTRODUCTION TO JAVA SCRIPT
 Java script is one of the popular scripting languages in
use today
 It allows us to embed (combine ) our HTML programs in
web pages and run these programs in the web browser.
 JAVA SCRIPT is also called as JSCRIPT
 It was created by Netscape
 Microsoft version of java script is called as JSCRIPT
 Internet explorer or any browser contains the “java
script interpreter” which processes the commands of a
script written in java script
INTRODUCTION TO JAVA SCRIPT (CONTD….)
 Scripting means a small sneak (piece). It is always
independent on other languages.
 There are no relationship between in java & java script
 Java Script is a scripting language that always dependent
in HTML language
 Java script is platform independent
 It is mainly used to creating DHTML (Dynamic HTML)
pages
JAVA SCRIPT IS EMBEDDED INTO HTML PAGES
USING:
< SCRIPT type = “text/javascript” >
……….
……….
</SCRIPT>
<script> tag indicates browser that the following
text is part of the script
 The script is containing 2 attributes. They are:
 Language attribute: - It represents name of scripting
language such as JavaScript, VbScript.
<script language=“JavaScript”>
 Type attribute: - It indicates MIME (multi purpose
internet mail extension) type of scripting code. It sets to
an alpha-numeric MIME type of code.
<script type=“text / JavaScript”>
 Location of script or placing the script: - Script code can
be placed in both head & body section of html page.
 Scripting in both head & body section: - we can create
unlimited number of scripts inside the same page. So we
can locate multiple scripts in both head & body section
of page.
PROGRAM TO DEMONSTRATE SCRIPT TAG
OUTPUT:
PROGRAM USING SCRIPT TAG IN BOTH HEAD
&BODY SECTIONS
OUTPUT:
JAVA SCRIPT OBJECTS
 Java script is object oriented language.
 Usage of objects make the java script programming very
easier.
 java script has number of pre-defined objects.
 Some of the objects are listed in the following table:
JAVA SCRIPT OBJECTS (CONTD….)
Name of the object Description
document Refers to the body of the current
page in browser, giving us an easy
way to access html code in that page.
form Holds information about html forms
in the current page
frame Refers to the frame in browser
window
location Holds information about location of
current web page such as its URL,
domain name etc.,,
navigator Refers to the browser it self, allowing
us to determine what the browser
has
window Refers to current browser window
JAVA SCRIPT OBJECTS (CONTD….)
 Every object has 2 important aspects:
1. Methods
2. Properties
Method of an object can be used by giving the object
name followed by dot (.) operator.
‘property’ holds some thing of an object
EXAMPLES OF METHODS
Method name Description
document.write Writes text to current web page
document.writeln Writes text to current web page and adds
carraige return
window.alert Displays and alert dialog box
window.open Opens a new browser window
clear( ) clears all the elements in the document
getElementByTag Name(“tag”) returns a collection of objects with the specified
TAGNAME
EXAMPLES OF PROPERTY
property description
document.bgoclor Background color of current page
document.fgoclor foreground color of current page
document.title Title of current page
lastModified returns the date & time the document was last
modified
title returns the title
URL returns the URL of the current document
Program to demonstrate document object and methods,
properties of it
<HTML>
<HEAD>
<TITLE> document </TITLE>
<script>
function title1( )
{
window.alert("Title of the document is :"+document.title);
}
function address( )
{
window.alert("URL of the document is :"+document.URL);
}
function bgcolor( )
{
document.bgColor="yellow";
}
Program to demonstrate document object and methods,
properties of it (contd…..)
function mdate( )
{
window.alert("The last modified date is :"+document.lastModified);
}
</script>
</HEAD>
<BODY>
<input type="button" value="page title" onclick="title1( )"><br>
<b onmouseover="address( )">to get page address just touch this text
</b><br>
<input type="button"value="change bgcolor" onclick="bgcolor( )"> <br>
<input type="button" value="Last modified date" onclick="mdate( )">
</BODY>
</HTML>
Output
EVENTS IN JAVA SCRIPT
 Apart from methods and properties one more important
concept in java script is EVENTS.
 DHTML makes our web pages come alive and allows us
to respond to the action of users.
 But how to know that an action has occurred?
 To inform us when some thing is happened java script
uses EVENTS.
 Some examples of events are mentioned in the
following table:
EVENTS IN JAVA SCRIPT (CONTD…..)
Name of the event Description
onclick Occurs when an element is clicked
ondbclick Occurs when an element is double
clicked
onload Occurs when page loads
onmousedown Occurs when mouse button goes
down
onmousemove Occurs when mouse moves
onmouseout Occurs when mouse leaves an
element
onmouseover Occurs when mouse moves over an
element
onmouseup Occurs when mouse button goes up
EVENTS IN JAVA SCRIPT (CONTD…..)
Name of event description
onselect Occurs when a selection takes place
onsubmit Occurs when user clicks the select
button
Program for handling mouse events
<HTML>
<HEAD>
<TITLE> Mouse Events </TITLE>
<script language="JavaScript">
function add()
{
a=55
b=45
c=a+b
document.write("addition is :"+c)
}
function sub()
{
a=55
b=45
c=a-b
document.write("subtraction is :"+c)
}
Program for handling mouse events (contd….)
</script>
</HEAD>
<BODY>
<b onclick="add( )">
to call function click here :
</b>
<br>
<b onmouseover="add( )">
to call function touch here :
</b>
<br>
<b ondblclick="add( )">
to call function double click here :
</b>
<br>
Program for handling mouse events (contd….)
<b onmousemove="add( )">
to call function cursor move here :
</b>
<br>
<b onmouseup="sub( )">
to call function cursor up here :
</b>
<br>
<b onmouseout="sub( )">
to call function cursor out here :
</b>
</BODY>
</HTML>
Output
WRITE JAVA SCRIPT TO PRINT A SINGLE
STATEMENT IN MULTIPLE LINES
OUTPUT
WRITE JAVA SCRIPT TO PRINT MULTIPLE LINES
IN DIALOG BOX
OUTPUT
WRITE A JAVA SCRIPT TO ADD 2 INTEGERS
OUTPUT
JAVASCRIPT SYNTAX RULES
 JavaScript is case sensitive language. In this upper case
lower case letters are differentiated (not same).
 Ex: - a=20;
 A=20;
 Those the variable name ‘a’ is different from the variable
named ‘A’.
 Ex: - myf( ) // correct
 myF( ) // incorrect
 ; is optional in general JavaScript.
JAVASCRIPT SYNTAX RULES (CONTD….)
 Ex: - a=20 // valid
b=30 // valid
A=10; b=40; // valid
 However it is required when you put multiple
statements in the same line.
 JavaScript ignore white space. In java script white
space, tag space & empty lines are not preserved.
 To display special symbols we use .
COMMENTS IN JAVA SCRIPT
 comments lines are not executable.
 // single line comment
 /* this is multi line comment */
DECLARING VARIABLES IN JAVA SCRIPT
 variable is a memory location where data can be stored.
 In java script variables with any type of data are
declared by using the keyword ‘var’.
 All keywords shall be in small letters only.
 var a; a=20;
 var str; str= “abc”;
 var c; c=’a’;
 var d; d=30.7;
 c;  not valid
DECLARING VARIABLES IN JAVA
SCRIPT(CONTD….)
 During the script, we can change value of variable as
well as type of value of variable.
 Ex: -
 a=20;
 a=30.7;
OPERATORS IN JAVA SCRIPT
 when ever you combined operators & operands then it
is called operation.
 X+Y ……… in this expression + is operator ; X and Y are
operands.
 Various kinds of operators that can be used in java script
are as follows:-
OPERATORS IN JAVA SCRIPT( CONTD….)
Name of operator symbols description
Arithmetic operators + , - , * , / , % Performs basic arithmetic
operations like addition,
subtraction etc.,,
Relational operators < , > , > = , < = , =
=
When you compare two
quantities, we can use relational
operators
Logical operators && , || , ! To Compare two or more
relational expressions logical
operators are used.
OPERATORS IN JAVA SCRIPT( CONTD….)
Name of operator symbols description
Conditional operators ?: ( ternary operator) var1=(cond)?var2:var3
Assignment operators = Assigns values to
variables
Short hand
assignment operators
+=, -=, *=, /= etc.,, These sets of operators
are compound
assignment operators &
combine couple of the
statements.
New operator var obj=new classname( ) new operator is creating
one object dynamically
Unary &binary
operators
++ , -- Acts on only one
operand
RULES OF OPERATOR PRECEDENCE
( ) --------- highest precedence
*, / , %
+ , -
=
CONTROL STRUCTURES IN JAVA SCRIPT
 Sequence structures
 Selection structures
 Repetition structures
SEQUENCE STURCTURES:
Unless directed these statements are executed one
after the another in the order in which they are written.
SELECTION STRUCTURES
(DECISION MAKING STATEMENTS)
 Simple ‘if’ statement
 If-else statement
 Nested-if statement
 Switch statement
 if statement is useful to perform(execute) a single
statement or group of statements when the condition is
true.
 if – else statement is useful to execute a one block of
code from two blocks where one condition.
 Switch statement is very frequently to select one of
many blocks of code for execution.
REPETITION STRUCTURES
( TO EXECUTE STATEMENTS REPEATEDLY)
 While loop
 Do-while loop
 For loop
 while loop is useful to execute a block of code
repeatedly based on condition. It is also entry controlled
loop.
 Do-while loop executes set of statements atleast once
even though the condition is false. It is also called as exit
controlled loop.
REPETITION STRUCTURES (CONTD…..)
 for loop has 3 statements in it:
for(initialization; cond; (inc / dec))
{
------
}
EXAMPLE PROGRAMS ON CONTROL
STRUCTURES
EXAMPLE PROGRAMS ON CONTROL
STRUCTURES (CONTD…..)
EXAMPLE PROGRAMS ON CONTROL
STRUCTURES (CONTD…..)
EXAMPLE PROGRAMS ON CONTROL
STRUCTURES (CONTD…..) USER VALIDATION
OUTPUT
EXAMPLE PROGRAMS ON CONTROL
STRUCTURES (CONTD…..) SWITCH STATEMENT
OUTPUT
FUNCTIONS IN JAVA SCRIPT
 In java script functions are created with the keyword
‘function’ as shown below
 Syntax: - function functionname( )
{
--------
}
FUNCTIONS IN JAVA SCRIPT (contd…..)
 Generally we can place the script containing function in
the head section of web page.
 There are 2 ways to call the function
1. Direct call function
2. Events handlers to call the function dynamically.
We can pass data to function as argument but that
data will be available inside the function.
<HTML>
<HEAD><TITLE> Function direct call</TITLE>
<script language="JavaScript">
function add(x,y)
{
z=x+y
return z
}
</script>
</HEAD>
<BODY>
<script>
var r=add(30,60)
document.write("addition is :"+r);
</script>
</BODY>
</HTML>
To add dynamical effects, java script provide a list of events that call
function dynamically. Here each event is one attribute that always
specified in html tags.
<HTML>
<HEAD>
<TITLE> Function dynamically</TITLE>
<script language="JavaScript">
function add( )
{
x=20
y=30
z=x+y
document.write("addition is :"+z);
}
</script>
</HEAD>
<BODY> to call function:
<input type="button" value="click here" onclick="add( )">
</script>
</BODY>
</HTML>
Program to demonstrate “tag” property
<HTML>
<HEAD>
<TITLE> see tag </TITLE>
<script>
function getElement( )
{
var x=document.getElementById("marqueeid");
window.alert("i am a "+x.tagName+"element")
}
</script>
</HEAD>
<BODY>
<marquee id="marqueeid“ onclick="getElement( )">
<b><b>
Click to see what element I am i
</marquee>
</BODY>
</HTML>
Output
Program to demonstrate open() and close()
<HTML>
<HEAD>
<TITLE> This is main page </TITLE>
<script>
function createNewDoc( )
{
var newDoc=document.open( )
var txt="<html><head><title>This is new document </title> </head><body>
WELCOME TO MVSREC </body> </html>";
newDoc.write(txt)
newDoc.close();
}
</script>
</HEAD>
<BODY onclick="createNewDoc( )">
<b>
To see the next page click here
</b>
</BODY>
</HTML>
Output
POPUP BOXES IN JAVA SCRIPT
 popup (arises) box is a small window that always
shown before opening the page.
 The purpose of popup box is to write message, accept
some thing from user.
 Java script provides 3 types of popup boxes. They are
1) Alert
2) Confirm
3) Prompt.
alert popup box
 Alert box is a very frequently useful to send or write
cautionary messages to end use
 Alert box is created by alert method of window object
as shown below.
 Syntax: - window – alert (“message”);
 When alert popup, the user has to click ok before
continue browsing.
Output
confirm popup box
 This is useful to verify or accept some thing from user.
 It is created by confirm method of window object as
shown below.
 Syntax:- window.confirm (“message?”);
 When the confirm box pop’s up, user must click either
ok or cancel buttons to proceed. If user clicks ok button
it returns the boolean value true. If user clicks cancel
button, it returns the boolean value false.
Program for confirm pop up box
<HTML>
<HEAD>
<TITLE> Confirm </TITLE>
<script>
function sub( )
{
a=50
b=45
c=a-b
x=window.confirm("Do you want to see subtraction of numbers")
if(x==true)
{
document.write("result is :"+c)
}
Program for confirm pop up box (contd….)
else
{
document.write("you clicked cancel button")
}
}
</script>
</HEAD>
<BODY onload="sub( )">
to see the o/p in pop up box:
</BODY>
</HTML>
Output
Prompt popup box
 It is useful to accept data from keyboard at runtime.
 Prompt box is created by prompt method of window object.
 Syntax: window.prompt (“message”, “default text”);
 When prompt dialog box arises user will have to click either ok
button or cancel button after entering input data to proceed. If
user click ok button it will return input value. If user click cancel
button the value “null” will be returned.
Program to demonstrate prompt popup box
<HTML>
<HEAD>
<TITLE> Prompt </TITLE>
<script>
function fact( )
{
var b=window.prompt("enter +ve integer :","enter here")
var c=parseInt(b)
a=1
for(i=c;i>=1;i--)
{
a=a*i
}
window.alert("factorial value :"+a)
}
</script>
</HEAD> <BODY onload="fact( )">
</BODY>
</HTML>
Output
STRINGS IN JAVA SCRIPT
 It is used to store text String that lets you to do process on it.
 There are 2 ways to store that string as shown below
1) var str=“KARTHIK”
2) var str=new String(“KARTHIK”)
 String object contains so many methods & properties.
 Representation: Objname.method( )
Methods in string class
Method name Description
big() displays a string in a big font
small() displays a string in a small font
blink() displays a blinking string
bold() displays a string in bold.
italics() displays a string in italic.
fontsize() displays a string in specific size.
fontcolor() displays a string in specified color.
strike() displays a string with a strike through.
sub() displays a string as a subscript
sup() displays a string as a super script.
Methods in string class (contd…..)
Method name Description
toLowerCase( ) displays a string in lower case letters
toUpperCase( ) displays a string in upper case letters
charAt( ) returns the character at a specified position
concat( ) join 2 or more strings
indexOf( ) returns the position of the 1st
occurrence of a
Specified string value in a string
lastIndexOf( ) returns the position of the last occurrence of a
Specified string value in a string
link( ) displays a string as a hyper link.
match( ) Searches for a specified string value in a string
and returns the sub string
replace( ) replaces some characters with another specified
characater
search( ) searches a string for a specified value.
Methods in string class (contd…..)
Method name Description
slice( ) extracts a part of a string and returns the
extracted part in a new string
spilt( ) splits a string in an array of strings.
substr( )
Extracts specified number of characters in a
string from starting index
subString( )
extracts character in a string in between 2
specified indices
Property of string class
 Length :
returns the no. of characters in a string.
Program to demonstrate string methods
<HTML>
<HEAD>
<TITLE> String class</TITLE>
</HEAD>
<BODY>
<script language="JavaScript">
var t=new String("karthik")
var s=new String("karthik")
document.write("<p>"+"Small = "+t.small( )+"</p>")
document.write("<p>"+"Blink = "+t.blink( )+"</p>")
document.write("<p>"+"Bold = "+t.bold( )+"</p>")
document.write("<p>"+"Italics = "+t.italics( )+"</p>")
document.write("<p>"+"Font size = "+t.fontsize(20)+"</p>")
document.write("<p>"+"Fixed = "+t.fixed( )+"</p>")
document.write("<p>"+"Font color = "+t.fontcolor("red")+"</p>")
document.write("<p>"+"strike = "+t.strike( )+"</p>")
document.write("<p>"+"Sub class = "+t.sub( )+"</p>")
document.write("<p>"+"Super Class = "+t.sup ( )+"</p>")
Program to demonstrate string methods
(contd..)
document.write("<p>"+"Lower case = "+t.toLowerCase( )+"</p>")
document.write("<p>"+"Upper case = "+t.toUpperCase( )+"</p>")
document.write("<p>"+"Char at = "+t.charAt( )+"</p>")
document.write("<p>"+"Concat = "+t.concat( )+"</p>")
document.write("<p>"+"Index of = "+t.indexOf("Reddy")+"</p>")
document.write("<p>"+"Last index of ="+t.lastIndexOf("Sudheer")+"</p>")
document.write("<p>"+"Link = "+t.link("www.google.com")+"</p>")
document.write("<p>"+"Match = "+s.match("Sunil")+"</p>")
document.write("<p>"+"Replace = "+t.replace("sunil kumar reddy")+"</p>")
document.write("<p>"+"Search = "+t.search("Sudheer")+"</p>")
document.write("<p>"+"Slice = "+s.slice("Sudheer")+"</p>")
document.write("<p>"+"Split = "+t.split("Reddy")+"</p>")
document.write("<p>"+"Substr ="+t.substr("Sunil")+"</p>")
document.write("<p>"+"Substring = "+t.substring("Sunil")+"</p>")
</script>
</BODY>
</HTML>
Output
ARRAYS IN JAVA SCRIPT
 The array class is used to store a set of values in a
single variable name with unique index number
 We can define an Array object with the operator new.
There are two ways of adding values to an array
 1) a) var myCars=new Array( );
myCars[0] = “Wagner”
myCars[1] = “innova”
myCars[2] = “Santro”
 You can also pass an integer argument to control the
array’s size.
ARRAYS IN JAVA SCRIPT (contd….)
 b) var my cars =new Array(3)
myCars[0] = “Wagner”
myCars[1] = “innova”
myCars[2] = “santro”
var myCars = new Array ( “Wagner”, “innova”, “Santro”)
 Array object contains so many methods & properties.
 Representation: Objname.method( )
Methods in array
concat( ) :
joins 2 or more arrays and returns the result.
join ( ) :
puts all the elements of an array into a string.
The elements are seperated by a specified delimiter
pop( ) :
removes & return the last element of an array
push( ) :
adds 1 or more elements to the end of an array & returns
the new length
Methods in array (contd….)
shift( ) :
removes & returns the first element of an array
unshift( ) :
adds 1 or more elements to the beginning of an array & returns the new
length
reverse( ) :
reverse the order of the elements in an array
slice( ) :
returns selected element from an existing array
Methods in array (contd….)
sort( ) :
sorts the array elements of an array.
splice( ) :
removes & adds new elements to an array
Property of array
length
sets or return the no. of elements in an array.
Program to demonstrate methods of array
<HTML>
<HEAD>
<TITLE> Array class </TITLE>
</HEAD>
<BODY>
<script language="JavaScript">
var a=new Array(3);
a[0]="abc"
a[1]="xyz"
a[2]="pqr"
var b=new Array(3);
b[0]="efg"
b[1]="uvw"
b[2]="lmn"
Program to demonstrate methods of array
(contd....)
for(i=0;i<a.length;i++)
{
document.write("<p>"+a[i]+"</p>")
}
document.write("<p>"+"Concat = "+a.concat(b)+"</p>")
document.write("<p>"+"Joined = "+a.join("-------- ")+"</p>")
document.write("<p>"+"Poped = "+a.pop( )+"</p>")
document.write("<p>"+"Poped ="+a.pop( )+"</p>")
document.write("<p>"+"Shift ="+a.shift( )+"</p>")
document.write("<p>"+"Unshift ="+b.unshift()+"</p>")
document.write("<p>"+"Sort ="+b.sort( )+"</p>")
document.write("<p>"+"Reverse ="+b.reverse( )+"</p>")
Program to demonstrate methods of array
(contd....)
document.write("<p>"+"Slice ="+b.slice( )+"</p>")
document.write("<p>"+"Pushed ="+a.push("abc")+"</p>")
document.write("<p>"+"pushed ="+a.push("xyz")+"</p>")
document.write("<p>"+"pushed ="+a.push("pqr")+"</p>")
document.write("<p>"+"Sort ="+a.sort( )+"</p>")
for(i=0;i<a.length;i++)
{
document.write("<p>"+a[i]+"</p>")
}
</script>
</BODY>
</HTML>
Output
Date class in java script
 The date class used to work with dates & time
 Syntax to create date object is
var d=new Date( );
Methods present in the date class are as follows:
Methods in date class
Method Description
 Date( ) returns today’s date & time.
 getDate( ) returns the day of the month.
 getDay( ) returns the day of the week (From 0 – 6).
 getMonth( ) returns the month (from 0 – 11)
 getFullYear( ) returns the year as a 4 digit number.
 getHours( ) returns the hour (from 0 - 23).
 getMinutes( ) returns the minutes (from 0 – 59).
 getSeconds( ) returns the seconds (from 0 – 59).
 getMilleSeconds( ) returns the milliseconds (from 0 – 999).
 getTime( ) returns the no. of milliseconds since midnight
jan 1,1970.
 setDate( ) sets the day of the month.
 setMonth( ) set the month (0 -11)
 setFullYear( ) sets the year (4 digits)
 setHours( ) set the Hour (0 – 23)
 setMinutes( ) set the minutes (0 - 59)
 setSeconds( ) set the seconds (0 – 59)
Program to demonstrate date class
<html>
<head>
<title> Date class </title>
</head>
<body>
<script type="text/JavaScript">
document.write("<b>today date</b>:"+Date( )+"<br>")
// to calculate years from 1970
var minutes=1000*60
hours=minutes*60
days=hours*24
years=days*365
var d=new Date( )
var t=d.getTime( )
var y=t,years
Program to demonstrate date class (contd….)
document.write("it's been: "+y +“days since
1970/01/01!"+"<br>")
// to set a specific date
var d=new Date( )
d.setFullYear(2007,8,19)
document.write("set specific date:" +d+"<br>")
// an array to write a week day & not just a number
var d=new Date( )
var weekday=new Array(7)
weekday[0]="Sunday"
weekday[1]="Monday"
weekday[2]="Tuesday"
Program to demonstrate date class (contd….)
weekday[3]="Wednesday"
weekday[4]="Thursday"
weekday[5]="Friday"
weekday[6]="Saturday"
document.write("Today it is "+weekday[d.getDay( )])
</script>
</body>
</html>
Output
Math class in java script
 The math class allows you to perform common mathematical
tasks
 Note: - it is not required to create object to math class before
using it.
 We can call methods of math class as shown below:
Math.methodName( )
Methods in math class
Method Description
 abs(x) returns the absolute value of a number
 ceil(x) returns the value of a number round
upwards to the nearest integer.
 floor(x) returns the value number rounded
downwards to the nearest integer.
 exp(x) returns the value of exponent.
 log(x) returns the natural algorithm (base E) of a
no.
 max(x,y) returns the number with the heights value
 min(x,y) returns the number with the lowest value.
 pow(x,y) returns the value of x to the power of y.
 random( ) return a random number between 0 & 1
 round(x) rounds a number to the nearest integer
1,2,3
sqrt(x) returns the squire root of a number.
 sin(x) returns the sine of an angle.
 cos(x) returns the cosine value of an angle.
 tan(x) returns the tangent of an angle.
Program to demonstrate math class methods
<html>
<head>
<title> Math class </title>
<body>
<script language="JavaScript">
x=4.6;
y=9;
document.write("<b>absolute="+Math.abs(x)+"<br>")
document.write("<b>ceil="+Math.ceil(x)+"<br>");
document.write("<b>floor="+Math.floor(x)+"<br>");
document.write("<b>exponent="+Math.exp(x)+"<br>");
document.write("<b>logarithm="+Math.log(x)+"<br>");
document.write("<b>maximum="+Math.max(x,y)+"<br>");
document.write("<b>minimum="+Math.min(x,y)+"<br>");
Program to demonstrate math class methods
(contd….)
document.write("<b>power="+Math.pow(y,x)+"<br>");
document.write("<b>random="+Math.random(x)+"<br>");
document.write("<b>round="+Math.round(x)+"<br>");
document.write("<b>squire="+Math.sqrt(x)+"<br>");
document.write("<b>sin="+Math.sin(x)+"<br>");
document.write("<b>cos="+Math.cos(x)+"<br>");
document.write("<b>tan="+Math.tan(x)+"<br>");
</script>
</body>
Output

More Related Content

PPTX
Java script
PPTX
Javascript
PPTX
Java script
PPT
Scripting languages
PPTX
Java Script basics and DOM
PPT
Javascript sivasoft
PPTX
JavaScripts & jQuery
PDF
Iwt note(module 2)
Java script
Javascript
Java script
Scripting languages
Java Script basics and DOM
Javascript sivasoft
JavaScripts & jQuery
Iwt note(module 2)

Similar to JAVA SCRIPT.pptbbdndndmdndndndndnndmmddnndn (20)

PPTX
Java script
PPTX
Client side scripting using Javascript
PDF
JAVA SCRIPT
PPTX
Javascript
PPTX
JavaScript with Syntax & Implementation
PPTX
Javascript
PPTX
Java script
DOC
Active browser web page
DOCX
JavaScript
PPTX
Web designing unit 4
PPTX
Java script
PPTX
Lecture 5 javascript
PDF
javascriptPresentation.pdf
PDF
Java Script notes
PPTX
wp-UNIT_III.pptx
PPTX
BITM3730 10-3.pptx
PPTX
BITM3730 10-4.pptx
PDF
java-scriptcdvcx vnbm,azsdfghjkml;sxdfcgmndxfcgvhb nmfctgvbhjnm ,cfgvb nm,xc ...
PPTX
Basics of Javascript
Java script
Client side scripting using Javascript
JAVA SCRIPT
Javascript
JavaScript with Syntax & Implementation
Javascript
Java script
Active browser web page
JavaScript
Web designing unit 4
Java script
Lecture 5 javascript
javascriptPresentation.pdf
Java Script notes
wp-UNIT_III.pptx
BITM3730 10-3.pptx
BITM3730 10-4.pptx
java-scriptcdvcx vnbm,azsdfghjkml;sxdfcgmndxfcgvhb nmfctgvbhjnm ,cfgvb nm,xc ...
Basics of Javascript
Ad

Recently uploaded (20)

PDF
Paper A Mock Exam 9_ Attempt review.pdf.
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PPTX
Radiologic_Anatomy_of_the_Brachial_plexus [final].pptx
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
LDMMIA Reiki Yoga Finals Review Spring Summer
PDF
Weekly quiz Compilation Jan -July 25.pdf
PDF
A systematic review of self-coping strategies used by university students to ...
PDF
SOIL: Factor, Horizon, Process, Classification, Degradation, Conservation
PDF
1_English_Language_Set_2.pdf probationary
PDF
IGGE1 Understanding the Self1234567891011
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
RMMM.pdf make it easy to upload and study
PPTX
Lesson notes of climatology university.
PPTX
Digestion and Absorption of Carbohydrates, Proteina and Fats
PDF
advance database management system book.pdf
PDF
Computing-Curriculum for Schools in Ghana
PDF
Complications of Minimal Access Surgery at WLH
Paper A Mock Exam 9_ Attempt review.pdf.
Supply Chain Operations Speaking Notes -ICLT Program
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
Radiologic_Anatomy_of_the_Brachial_plexus [final].pptx
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
LDMMIA Reiki Yoga Finals Review Spring Summer
Weekly quiz Compilation Jan -July 25.pdf
A systematic review of self-coping strategies used by university students to ...
SOIL: Factor, Horizon, Process, Classification, Degradation, Conservation
1_English_Language_Set_2.pdf probationary
IGGE1 Understanding the Self1234567891011
Final Presentation General Medicine 03-08-2024.pptx
RMMM.pdf make it easy to upload and study
Lesson notes of climatology university.
Digestion and Absorption of Carbohydrates, Proteina and Fats
advance database management system book.pdf
Computing-Curriculum for Schools in Ghana
Complications of Minimal Access Surgery at WLH
Ad

JAVA SCRIPT.pptbbdndndmdndndndndnndmmddnndn

  • 2. REASON FOR USING JAVA SCRIPT  HTML is suitable for developing web pages  Web pages that are developed using html are STATIC web pages  That is, in html web pages we can display and format text, insert links and images etc.,,  But these web pages will have the same look every time it is executed.  There is no interaction with the web page
  • 3. REASON FOR USING JAVA SCRIPT (CONTD…..)  In order to increase this interaction we must integrate these web pages with some programming techniques.  To add this interactivity to HTML web pages JAVA SCRIPT is used
  • 4. INTRODUCTION TO JAVA SCRIPT  Java script is one of the popular scripting languages in use today  It allows us to embed (combine ) our HTML programs in web pages and run these programs in the web browser.  JAVA SCRIPT is also called as JSCRIPT  It was created by Netscape  Microsoft version of java script is called as JSCRIPT  Internet explorer or any browser contains the “java script interpreter” which processes the commands of a script written in java script
  • 5. INTRODUCTION TO JAVA SCRIPT (CONTD….)  Scripting means a small sneak (piece). It is always independent on other languages.  There are no relationship between in java & java script  Java Script is a scripting language that always dependent in HTML language  Java script is platform independent  It is mainly used to creating DHTML (Dynamic HTML) pages
  • 6. JAVA SCRIPT IS EMBEDDED INTO HTML PAGES USING: < SCRIPT type = “text/javascript” > ………. ………. </SCRIPT> <script> tag indicates browser that the following text is part of the script
  • 7.  The script is containing 2 attributes. They are:  Language attribute: - It represents name of scripting language such as JavaScript, VbScript. <script language=“JavaScript”>  Type attribute: - It indicates MIME (multi purpose internet mail extension) type of scripting code. It sets to an alpha-numeric MIME type of code. <script type=“text / JavaScript”>
  • 8.  Location of script or placing the script: - Script code can be placed in both head & body section of html page.  Scripting in both head & body section: - we can create unlimited number of scripts inside the same page. So we can locate multiple scripts in both head & body section of page.
  • 11. PROGRAM USING SCRIPT TAG IN BOTH HEAD &BODY SECTIONS
  • 13. JAVA SCRIPT OBJECTS  Java script is object oriented language.  Usage of objects make the java script programming very easier.  java script has number of pre-defined objects.  Some of the objects are listed in the following table:
  • 14. JAVA SCRIPT OBJECTS (CONTD….) Name of the object Description document Refers to the body of the current page in browser, giving us an easy way to access html code in that page. form Holds information about html forms in the current page frame Refers to the frame in browser window location Holds information about location of current web page such as its URL, domain name etc.,, navigator Refers to the browser it self, allowing us to determine what the browser has window Refers to current browser window
  • 15. JAVA SCRIPT OBJECTS (CONTD….)  Every object has 2 important aspects: 1. Methods 2. Properties Method of an object can be used by giving the object name followed by dot (.) operator. ‘property’ holds some thing of an object
  • 16. EXAMPLES OF METHODS Method name Description document.write Writes text to current web page document.writeln Writes text to current web page and adds carraige return window.alert Displays and alert dialog box window.open Opens a new browser window clear( ) clears all the elements in the document getElementByTag Name(“tag”) returns a collection of objects with the specified TAGNAME
  • 17. EXAMPLES OF PROPERTY property description document.bgoclor Background color of current page document.fgoclor foreground color of current page document.title Title of current page lastModified returns the date & time the document was last modified title returns the title URL returns the URL of the current document
  • 18. Program to demonstrate document object and methods, properties of it <HTML> <HEAD> <TITLE> document </TITLE> <script> function title1( ) { window.alert("Title of the document is :"+document.title); } function address( ) { window.alert("URL of the document is :"+document.URL); } function bgcolor( ) { document.bgColor="yellow"; }
  • 19. Program to demonstrate document object and methods, properties of it (contd…..) function mdate( ) { window.alert("The last modified date is :"+document.lastModified); } </script> </HEAD> <BODY> <input type="button" value="page title" onclick="title1( )"><br> <b onmouseover="address( )">to get page address just touch this text </b><br> <input type="button"value="change bgcolor" onclick="bgcolor( )"> <br> <input type="button" value="Last modified date" onclick="mdate( )"> </BODY> </HTML>
  • 21. EVENTS IN JAVA SCRIPT  Apart from methods and properties one more important concept in java script is EVENTS.  DHTML makes our web pages come alive and allows us to respond to the action of users.  But how to know that an action has occurred?  To inform us when some thing is happened java script uses EVENTS.  Some examples of events are mentioned in the following table:
  • 22. EVENTS IN JAVA SCRIPT (CONTD…..) Name of the event Description onclick Occurs when an element is clicked ondbclick Occurs when an element is double clicked onload Occurs when page loads onmousedown Occurs when mouse button goes down onmousemove Occurs when mouse moves onmouseout Occurs when mouse leaves an element onmouseover Occurs when mouse moves over an element onmouseup Occurs when mouse button goes up
  • 23. EVENTS IN JAVA SCRIPT (CONTD…..) Name of event description onselect Occurs when a selection takes place onsubmit Occurs when user clicks the select button
  • 24. Program for handling mouse events <HTML> <HEAD> <TITLE> Mouse Events </TITLE> <script language="JavaScript"> function add() { a=55 b=45 c=a+b document.write("addition is :"+c) } function sub() { a=55 b=45 c=a-b document.write("subtraction is :"+c) }
  • 25. Program for handling mouse events (contd….) </script> </HEAD> <BODY> <b onclick="add( )"> to call function click here : </b> <br> <b onmouseover="add( )"> to call function touch here : </b> <br> <b ondblclick="add( )"> to call function double click here : </b> <br>
  • 26. Program for handling mouse events (contd….) <b onmousemove="add( )"> to call function cursor move here : </b> <br> <b onmouseup="sub( )"> to call function cursor up here : </b> <br> <b onmouseout="sub( )"> to call function cursor out here : </b> </BODY> </HTML>
  • 28. WRITE JAVA SCRIPT TO PRINT A SINGLE STATEMENT IN MULTIPLE LINES
  • 30. WRITE JAVA SCRIPT TO PRINT MULTIPLE LINES IN DIALOG BOX
  • 32. WRITE A JAVA SCRIPT TO ADD 2 INTEGERS
  • 34. JAVASCRIPT SYNTAX RULES  JavaScript is case sensitive language. In this upper case lower case letters are differentiated (not same).  Ex: - a=20;  A=20;  Those the variable name ‘a’ is different from the variable named ‘A’.  Ex: - myf( ) // correct  myF( ) // incorrect  ; is optional in general JavaScript.
  • 35. JAVASCRIPT SYNTAX RULES (CONTD….)  Ex: - a=20 // valid b=30 // valid A=10; b=40; // valid  However it is required when you put multiple statements in the same line.  JavaScript ignore white space. In java script white space, tag space & empty lines are not preserved.  To display special symbols we use .
  • 36. COMMENTS IN JAVA SCRIPT  comments lines are not executable.  // single line comment  /* this is multi line comment */
  • 37. DECLARING VARIABLES IN JAVA SCRIPT  variable is a memory location where data can be stored.  In java script variables with any type of data are declared by using the keyword ‘var’.  All keywords shall be in small letters only.  var a; a=20;  var str; str= “abc”;  var c; c=’a’;  var d; d=30.7;  c;  not valid
  • 38. DECLARING VARIABLES IN JAVA SCRIPT(CONTD….)  During the script, we can change value of variable as well as type of value of variable.  Ex: -  a=20;  a=30.7;
  • 39. OPERATORS IN JAVA SCRIPT  when ever you combined operators & operands then it is called operation.  X+Y ……… in this expression + is operator ; X and Y are operands.  Various kinds of operators that can be used in java script are as follows:-
  • 40. OPERATORS IN JAVA SCRIPT( CONTD….) Name of operator symbols description Arithmetic operators + , - , * , / , % Performs basic arithmetic operations like addition, subtraction etc.,, Relational operators < , > , > = , < = , = = When you compare two quantities, we can use relational operators Logical operators && , || , ! To Compare two or more relational expressions logical operators are used.
  • 41. OPERATORS IN JAVA SCRIPT( CONTD….) Name of operator symbols description Conditional operators ?: ( ternary operator) var1=(cond)?var2:var3 Assignment operators = Assigns values to variables Short hand assignment operators +=, -=, *=, /= etc.,, These sets of operators are compound assignment operators & combine couple of the statements. New operator var obj=new classname( ) new operator is creating one object dynamically Unary &binary operators ++ , -- Acts on only one operand
  • 42. RULES OF OPERATOR PRECEDENCE ( ) --------- highest precedence *, / , % + , - =
  • 43. CONTROL STRUCTURES IN JAVA SCRIPT  Sequence structures  Selection structures  Repetition structures SEQUENCE STURCTURES: Unless directed these statements are executed one after the another in the order in which they are written.
  • 44. SELECTION STRUCTURES (DECISION MAKING STATEMENTS)  Simple ‘if’ statement  If-else statement  Nested-if statement  Switch statement  if statement is useful to perform(execute) a single statement or group of statements when the condition is true.  if – else statement is useful to execute a one block of code from two blocks where one condition.  Switch statement is very frequently to select one of many blocks of code for execution.
  • 45. REPETITION STRUCTURES ( TO EXECUTE STATEMENTS REPEATEDLY)  While loop  Do-while loop  For loop  while loop is useful to execute a block of code repeatedly based on condition. It is also entry controlled loop.  Do-while loop executes set of statements atleast once even though the condition is false. It is also called as exit controlled loop.
  • 46. REPETITION STRUCTURES (CONTD…..)  for loop has 3 statements in it: for(initialization; cond; (inc / dec)) { ------ }
  • 47. EXAMPLE PROGRAMS ON CONTROL STRUCTURES
  • 48. EXAMPLE PROGRAMS ON CONTROL STRUCTURES (CONTD…..)
  • 49. EXAMPLE PROGRAMS ON CONTROL STRUCTURES (CONTD…..)
  • 50. EXAMPLE PROGRAMS ON CONTROL STRUCTURES (CONTD…..) USER VALIDATION
  • 52. EXAMPLE PROGRAMS ON CONTROL STRUCTURES (CONTD…..) SWITCH STATEMENT
  • 54. FUNCTIONS IN JAVA SCRIPT  In java script functions are created with the keyword ‘function’ as shown below  Syntax: - function functionname( ) { -------- }
  • 55. FUNCTIONS IN JAVA SCRIPT (contd…..)  Generally we can place the script containing function in the head section of web page.  There are 2 ways to call the function 1. Direct call function 2. Events handlers to call the function dynamically.
  • 56. We can pass data to function as argument but that data will be available inside the function. <HTML> <HEAD><TITLE> Function direct call</TITLE> <script language="JavaScript"> function add(x,y) { z=x+y return z } </script> </HEAD> <BODY> <script> var r=add(30,60) document.write("addition is :"+r); </script> </BODY> </HTML>
  • 57. To add dynamical effects, java script provide a list of events that call function dynamically. Here each event is one attribute that always specified in html tags. <HTML> <HEAD> <TITLE> Function dynamically</TITLE> <script language="JavaScript"> function add( ) { x=20 y=30 z=x+y document.write("addition is :"+z); } </script> </HEAD> <BODY> to call function: <input type="button" value="click here" onclick="add( )"> </script> </BODY> </HTML>
  • 58. Program to demonstrate “tag” property <HTML> <HEAD> <TITLE> see tag </TITLE> <script> function getElement( ) { var x=document.getElementById("marqueeid"); window.alert("i am a "+x.tagName+"element") } </script> </HEAD> <BODY> <marquee id="marqueeid“ onclick="getElement( )"> <b><b> Click to see what element I am i </marquee> </BODY> </HTML>
  • 60. Program to demonstrate open() and close() <HTML> <HEAD> <TITLE> This is main page </TITLE> <script> function createNewDoc( ) { var newDoc=document.open( ) var txt="<html><head><title>This is new document </title> </head><body> WELCOME TO MVSREC </body> </html>"; newDoc.write(txt) newDoc.close(); } </script> </HEAD> <BODY onclick="createNewDoc( )"> <b> To see the next page click here </b> </BODY> </HTML>
  • 62. POPUP BOXES IN JAVA SCRIPT  popup (arises) box is a small window that always shown before opening the page.  The purpose of popup box is to write message, accept some thing from user.  Java script provides 3 types of popup boxes. They are 1) Alert 2) Confirm 3) Prompt.
  • 63. alert popup box  Alert box is a very frequently useful to send or write cautionary messages to end use  Alert box is created by alert method of window object as shown below.  Syntax: - window – alert (“message”);  When alert popup, the user has to click ok before continue browsing.
  • 65. confirm popup box  This is useful to verify or accept some thing from user.  It is created by confirm method of window object as shown below.  Syntax:- window.confirm (“message?”);  When the confirm box pop’s up, user must click either ok or cancel buttons to proceed. If user clicks ok button it returns the boolean value true. If user clicks cancel button, it returns the boolean value false.
  • 66. Program for confirm pop up box <HTML> <HEAD> <TITLE> Confirm </TITLE> <script> function sub( ) { a=50 b=45 c=a-b x=window.confirm("Do you want to see subtraction of numbers") if(x==true) { document.write("result is :"+c) }
  • 67. Program for confirm pop up box (contd….) else { document.write("you clicked cancel button") } } </script> </HEAD> <BODY onload="sub( )"> to see the o/p in pop up box: </BODY> </HTML>
  • 69. Prompt popup box  It is useful to accept data from keyboard at runtime.  Prompt box is created by prompt method of window object.  Syntax: window.prompt (“message”, “default text”);  When prompt dialog box arises user will have to click either ok button or cancel button after entering input data to proceed. If user click ok button it will return input value. If user click cancel button the value “null” will be returned.
  • 70. Program to demonstrate prompt popup box <HTML> <HEAD> <TITLE> Prompt </TITLE> <script> function fact( ) { var b=window.prompt("enter +ve integer :","enter here") var c=parseInt(b) a=1 for(i=c;i>=1;i--) { a=a*i } window.alert("factorial value :"+a) } </script> </HEAD> <BODY onload="fact( )"> </BODY> </HTML>
  • 72. STRINGS IN JAVA SCRIPT  It is used to store text String that lets you to do process on it.  There are 2 ways to store that string as shown below 1) var str=“KARTHIK” 2) var str=new String(“KARTHIK”)  String object contains so many methods & properties.  Representation: Objname.method( )
  • 73. Methods in string class Method name Description big() displays a string in a big font small() displays a string in a small font blink() displays a blinking string bold() displays a string in bold. italics() displays a string in italic. fontsize() displays a string in specific size. fontcolor() displays a string in specified color. strike() displays a string with a strike through. sub() displays a string as a subscript sup() displays a string as a super script.
  • 74. Methods in string class (contd…..) Method name Description toLowerCase( ) displays a string in lower case letters toUpperCase( ) displays a string in upper case letters charAt( ) returns the character at a specified position concat( ) join 2 or more strings indexOf( ) returns the position of the 1st occurrence of a Specified string value in a string lastIndexOf( ) returns the position of the last occurrence of a Specified string value in a string link( ) displays a string as a hyper link. match( ) Searches for a specified string value in a string and returns the sub string replace( ) replaces some characters with another specified characater search( ) searches a string for a specified value.
  • 75. Methods in string class (contd…..) Method name Description slice( ) extracts a part of a string and returns the extracted part in a new string spilt( ) splits a string in an array of strings. substr( ) Extracts specified number of characters in a string from starting index subString( ) extracts character in a string in between 2 specified indices
  • 76. Property of string class  Length : returns the no. of characters in a string.
  • 77. Program to demonstrate string methods <HTML> <HEAD> <TITLE> String class</TITLE> </HEAD> <BODY> <script language="JavaScript"> var t=new String("karthik") var s=new String("karthik") document.write("<p>"+"Small = "+t.small( )+"</p>") document.write("<p>"+"Blink = "+t.blink( )+"</p>") document.write("<p>"+"Bold = "+t.bold( )+"</p>") document.write("<p>"+"Italics = "+t.italics( )+"</p>") document.write("<p>"+"Font size = "+t.fontsize(20)+"</p>") document.write("<p>"+"Fixed = "+t.fixed( )+"</p>") document.write("<p>"+"Font color = "+t.fontcolor("red")+"</p>") document.write("<p>"+"strike = "+t.strike( )+"</p>") document.write("<p>"+"Sub class = "+t.sub( )+"</p>") document.write("<p>"+"Super Class = "+t.sup ( )+"</p>")
  • 78. Program to demonstrate string methods (contd..) document.write("<p>"+"Lower case = "+t.toLowerCase( )+"</p>") document.write("<p>"+"Upper case = "+t.toUpperCase( )+"</p>") document.write("<p>"+"Char at = "+t.charAt( )+"</p>") document.write("<p>"+"Concat = "+t.concat( )+"</p>") document.write("<p>"+"Index of = "+t.indexOf("Reddy")+"</p>") document.write("<p>"+"Last index of ="+t.lastIndexOf("Sudheer")+"</p>") document.write("<p>"+"Link = "+t.link("www.google.com")+"</p>") document.write("<p>"+"Match = "+s.match("Sunil")+"</p>") document.write("<p>"+"Replace = "+t.replace("sunil kumar reddy")+"</p>") document.write("<p>"+"Search = "+t.search("Sudheer")+"</p>") document.write("<p>"+"Slice = "+s.slice("Sudheer")+"</p>") document.write("<p>"+"Split = "+t.split("Reddy")+"</p>") document.write("<p>"+"Substr ="+t.substr("Sunil")+"</p>") document.write("<p>"+"Substring = "+t.substring("Sunil")+"</p>") </script> </BODY> </HTML>
  • 80. ARRAYS IN JAVA SCRIPT  The array class is used to store a set of values in a single variable name with unique index number  We can define an Array object with the operator new. There are two ways of adding values to an array  1) a) var myCars=new Array( ); myCars[0] = “Wagner” myCars[1] = “innova” myCars[2] = “Santro”  You can also pass an integer argument to control the array’s size.
  • 81. ARRAYS IN JAVA SCRIPT (contd….)  b) var my cars =new Array(3) myCars[0] = “Wagner” myCars[1] = “innova” myCars[2] = “santro” var myCars = new Array ( “Wagner”, “innova”, “Santro”)  Array object contains so many methods & properties.  Representation: Objname.method( )
  • 82. Methods in array concat( ) : joins 2 or more arrays and returns the result. join ( ) : puts all the elements of an array into a string. The elements are seperated by a specified delimiter pop( ) : removes & return the last element of an array push( ) : adds 1 or more elements to the end of an array & returns the new length
  • 83. Methods in array (contd….) shift( ) : removes & returns the first element of an array unshift( ) : adds 1 or more elements to the beginning of an array & returns the new length reverse( ) : reverse the order of the elements in an array slice( ) : returns selected element from an existing array
  • 84. Methods in array (contd….) sort( ) : sorts the array elements of an array. splice( ) : removes & adds new elements to an array Property of array length sets or return the no. of elements in an array.
  • 85. Program to demonstrate methods of array <HTML> <HEAD> <TITLE> Array class </TITLE> </HEAD> <BODY> <script language="JavaScript"> var a=new Array(3); a[0]="abc" a[1]="xyz" a[2]="pqr" var b=new Array(3); b[0]="efg" b[1]="uvw" b[2]="lmn"
  • 86. Program to demonstrate methods of array (contd....) for(i=0;i<a.length;i++) { document.write("<p>"+a[i]+"</p>") } document.write("<p>"+"Concat = "+a.concat(b)+"</p>") document.write("<p>"+"Joined = "+a.join("-------- ")+"</p>") document.write("<p>"+"Poped = "+a.pop( )+"</p>") document.write("<p>"+"Poped ="+a.pop( )+"</p>") document.write("<p>"+"Shift ="+a.shift( )+"</p>") document.write("<p>"+"Unshift ="+b.unshift()+"</p>") document.write("<p>"+"Sort ="+b.sort( )+"</p>") document.write("<p>"+"Reverse ="+b.reverse( )+"</p>")
  • 87. Program to demonstrate methods of array (contd....) document.write("<p>"+"Slice ="+b.slice( )+"</p>") document.write("<p>"+"Pushed ="+a.push("abc")+"</p>") document.write("<p>"+"pushed ="+a.push("xyz")+"</p>") document.write("<p>"+"pushed ="+a.push("pqr")+"</p>") document.write("<p>"+"Sort ="+a.sort( )+"</p>") for(i=0;i<a.length;i++) { document.write("<p>"+a[i]+"</p>") } </script> </BODY> </HTML>
  • 89. Date class in java script  The date class used to work with dates & time  Syntax to create date object is var d=new Date( ); Methods present in the date class are as follows:
  • 90. Methods in date class Method Description  Date( ) returns today’s date & time.  getDate( ) returns the day of the month.  getDay( ) returns the day of the week (From 0 – 6).  getMonth( ) returns the month (from 0 – 11)  getFullYear( ) returns the year as a 4 digit number.  getHours( ) returns the hour (from 0 - 23).  getMinutes( ) returns the minutes (from 0 – 59).  getSeconds( ) returns the seconds (from 0 – 59).  getMilleSeconds( ) returns the milliseconds (from 0 – 999).  getTime( ) returns the no. of milliseconds since midnight jan 1,1970.  setDate( ) sets the day of the month.  setMonth( ) set the month (0 -11)  setFullYear( ) sets the year (4 digits)  setHours( ) set the Hour (0 – 23)  setMinutes( ) set the minutes (0 - 59)  setSeconds( ) set the seconds (0 – 59)
  • 91. Program to demonstrate date class <html> <head> <title> Date class </title> </head> <body> <script type="text/JavaScript"> document.write("<b>today date</b>:"+Date( )+"<br>") // to calculate years from 1970 var minutes=1000*60 hours=minutes*60 days=hours*24 years=days*365 var d=new Date( ) var t=d.getTime( ) var y=t,years
  • 92. Program to demonstrate date class (contd….) document.write("it's been: "+y +“days since 1970/01/01!"+"<br>") // to set a specific date var d=new Date( ) d.setFullYear(2007,8,19) document.write("set specific date:" +d+"<br>") // an array to write a week day & not just a number var d=new Date( ) var weekday=new Array(7) weekday[0]="Sunday" weekday[1]="Monday" weekday[2]="Tuesday"
  • 93. Program to demonstrate date class (contd….) weekday[3]="Wednesday" weekday[4]="Thursday" weekday[5]="Friday" weekday[6]="Saturday" document.write("Today it is "+weekday[d.getDay( )]) </script> </body> </html>
  • 95. Math class in java script  The math class allows you to perform common mathematical tasks  Note: - it is not required to create object to math class before using it.  We can call methods of math class as shown below: Math.methodName( )
  • 96. Methods in math class Method Description  abs(x) returns the absolute value of a number  ceil(x) returns the value of a number round upwards to the nearest integer.  floor(x) returns the value number rounded downwards to the nearest integer.  exp(x) returns the value of exponent.  log(x) returns the natural algorithm (base E) of a no.  max(x,y) returns the number with the heights value  min(x,y) returns the number with the lowest value.  pow(x,y) returns the value of x to the power of y.  random( ) return a random number between 0 & 1  round(x) rounds a number to the nearest integer 1,2,3 sqrt(x) returns the squire root of a number.  sin(x) returns the sine of an angle.  cos(x) returns the cosine value of an angle.  tan(x) returns the tangent of an angle.
  • 97. Program to demonstrate math class methods <html> <head> <title> Math class </title> <body> <script language="JavaScript"> x=4.6; y=9; document.write("<b>absolute="+Math.abs(x)+"<br>") document.write("<b>ceil="+Math.ceil(x)+"<br>"); document.write("<b>floor="+Math.floor(x)+"<br>"); document.write("<b>exponent="+Math.exp(x)+"<br>"); document.write("<b>logarithm="+Math.log(x)+"<br>"); document.write("<b>maximum="+Math.max(x,y)+"<br>"); document.write("<b>minimum="+Math.min(x,y)+"<br>");
  • 98. Program to demonstrate math class methods (contd….) document.write("<b>power="+Math.pow(y,x)+"<br>"); document.write("<b>random="+Math.random(x)+"<br>"); document.write("<b>round="+Math.round(x)+"<br>"); document.write("<b>squire="+Math.sqrt(x)+"<br>"); document.write("<b>sin="+Math.sin(x)+"<br>"); document.write("<b>cos="+Math.cos(x)+"<br>"); document.write("<b>tan="+Math.tan(x)+"<br>"); </script> </body>