3 Advanced Javascript
3 Advanced Javascript
3 Advanced Javascript
I Chapter at a Glance
Types of Scripting: •
CUont Sid" Scripting 4r Server SidIoSc'ripting.Differenre
between Client Side and Serv!'>rSide Stripting
[ 3.30bje<\s in
)avaStript
T
1
•
DOM (l)ocQment Qbject Modell: •
o.,iiniIion, Method and Properly of OOM obit"'t
Window Objecl : •
Definition, Method and I'toperty
• of Window object,
:P«)perly Jike head, titl", URL, bod)l, bllg, Propert}' like n~=, lot,ti'iol'l, document, status, closed,
Methedslike writeO, writoln(), g"tElemenlByldO Melhod like ilIlerl(j,confirm{i. promptO, opent), dQ$<>(j,
:wIth e~ample;;. blur{), tocus{), pdl'lt(), setTirneQulO with e~mllpl~
3.1 Introduction
1~ is an interpreted scripting language.
Ans. : JavaScript
2. . ; is a set of instructions used to produce various kinds of outputs.
Ans. : Program
3. The programs in javaScript language are called .
Ans. : Scripts . ,
4'. .. is an object oriented scripting language and supports event based
programming facility.
Ans. : JavaScript
5. .. is platform independent scripting language.
Ans.: JavaScript
6. .. is knows as Universal client side scripting language.
Ans.: JavaScript
7. In ~ scripting the script resides on the client computer.
Ans. : Client side
8. In scripting the script resides on web server.
Ans. : Server side
9. .. scripting does not need any server interaction.
Ans. : Client side
10. .. ~ scripting communicates to the server.
Ans. : Server side
11. .. scripts 'are used for validation purpose.
Ans. : Client side.
12. .. scripts requires web browser as on 'interface.
Ans. : Client side
13. .. , scripts requires web server software to execute.
Ans. : Server side
14. .. is a JavaScript based open source frontend web framework
developed for single page application.
Ans. : Angular JS
15. .. is JavaScript based framework for building interactive user
interface.
Ans.: Vue Js.
Tf'Sfnformation Technology (Science) 3-3 Advanced JavaScript
3.30bjeds in JavaScript
23. The keyword is used to create new object in JavaScript.
Ans. : new
24. An :..•........ can group data together with functions needed to manipulate
it.
Ans.: Object
25. All tangible things are known as .
Ans. : Objects
26. DaM stands for : .
Ans. : Document Object Model
27. The way in which HTML document content IS accessed and modified is called as
,.
TPS Information Technology (Science) 3-9 Advanced JavaScript
3.1 Introduction
1. JavaScript is an involved scripting language.
Ans.: False
2. An interpreted language is a. type of programming. language that executes its
instructions directly without compiling machine language.
Ans. : True
TPS Information Technology (Science) 3~10 Advanced JavaScript
21. Client side scripting does not need any server interaction.
Ans. ::J'rue
22. PHP, ASP.net etc are server side scripting languages.
Ans. : True
23. Client side scripts are generally used for validation purpose.
Ans.: True
37; Properties and methods of object are accessed with "," Operator.
Ans. : True
38. JavaScript supports 4 types of objects.
Ans.: False
39. JavaScript gives facility to create user defined objects.
Ans.: True
40. The new keyword is used to create new object in JavaScript.
Ans.: True
41. DOM stands for Document Original Model. .
Ans.: False
42. The way in which HTML document content is accessed and modified is ~called as
DOM.
Ans, : True
43. Head property of DOM object returns the <head> element of the document:
Ans.: True
44. Title property is DOM object returns the name of the document.
Ans.: False
45. URL property ofDOM object returns full URL of the HTML document.
Ans. : True
46. Body property of,DOM object returns <body> elements respectively.
Ans. : True
47. writet) method of DOM object write Java script code to a document.
Ans. : True
48. writelnO method is same as write ( ) method of DOM object.
Ans.: False
49. writelnt) method of DOM object adds a new line character after each statement.
Ans. : True
50. The outerHTML property is used to change any HTML element.
Ans.: False
51. window object is parent object of all other objects-.
Ans.: True
52. An object of window is created automatically by the browser.
Ans.: True
53. name property of window object sets or returns the name of a window.
Ans. : True
54. location property of window object returns a' Boolean value indicating whether a
window has been dosed or not.
Ans. : False
TPS Information Technology (Science) 3-13 Advanced JavaScript
55. document property of window object returns the document object for the window.
Ans. : True
56. . status property of window object sets the name of a window.
Ans.: False
57. alertrjmethod of window object displays the alert box containing message with ok
button.
Ans, :True
58. promptt) method of window object displays a dialog box to set output for the user.
Ans.: False
59. confirmf) method of window object displays the confirm dialog box containing
message with ok and cancel button:
Ans. : True
60. open( ) method of window object removes the focus from the current window.
Ans.: False
61. closet) method of window object closes the current window.
Ans.r True
62. blurt) method of window object removes focus from the current window.
Ans. : True
63. focusi) method of window object removes focus from the current window.
Ans.: False·
64. printO method of window object prints the content of current window .
. Ans. : True
65. setTimeoutO method of window. object calls a function after a specified number of
milliseconds.
Ans. : True
123. isFixedO method of number object returns the string that represents a number with
exact digits after a decimal point. .
Ans.: True
124. An array is an object that can store a collection of items.
Ans, : True
125. Arrays are used to store single value in singlevariable.
Ails.: False
126. Items of array can be accessed by referring to its index number.
Ails. .True
127. Indexof first element of an array is one.
Ans.: False
128. marks[O] is the first element ofthe array.
Ans. : True
129. Index property of Array object represents two based index of the matching the
string.
Ans.: False
130. Length property of Array object reflects number of elements in array.
Ans. : True
131. concatO method of Array object joins two or more arrays and returns a copy of
joined array.
Ans.: True
132. copyWithinO method of Array object adds array elements within the array to and
from a specified positions,
Ans.: False
133. find 0 method of Array object returns the value of the first element in art array that
satisfies a test in testing. .
Ans, : True
134. forEachO method of Array object search-the array and returns it's position.
Ans.: False
135. indexOfO method. of Array object search the array for an element and returns its
position. . .
Ans. : True
136.' isArray() method of Array object removes element from array.
Ans.: False
137. popljmethod of Array objects removes the last element of an array and returns that
element.
Ans.: True
138. pusht) method of Array object add new elements to the end of an array and returns
the new length .
. Ans.:True
TPS Information Technology (Science) 3-18 Advanced JavaScript
J'"
TPS Information Technology (Science) 3-21 Advanced JavaScript
Ans.: (b) .
TPS Information Technology (Science) 3-22 Advanced JavaScript
39. . ; property of window object returns the location object for the
window. .
(a) Window (b) Array (c) Math (d) Document
Ana: (d)
40. ........................•........ property of window .object returns the location object for the
window.
(a) name (b) location (c) status (d) closed
Ans.: (b)
41. . property of window object sets or returns the text in the status.
bar of a window. .
(a) name (b) location (c) status (d) closed
Ans.: (e)
42. . property of window object returns a Boolean valve indicating
whether a window has been closed or not.
(a) closed (b) status (c) document (d) name
Ans.: (a)
43. .. method of window object display the alert box containing
message with OK button.
(a) opent) (b) closet) (c) .alertf) . (d) promptt)
Ans.: (c)
44. .. method of window object displays the confirm dialog box
containing message with ok and cancel button.
(a) blurt) (b) confirnu) (c) printt) (d) focust)
Ans.: (b)
45. . : method 'of window object displays a dialog box to get input from the
user.
(a) alertt) (b) operu) (c) confirmt) (d) prompn)
Ans.: (d)
46. .. method of window object opens the new window.
(a) opent) (b) closet) (c) focust) (d) blurt)
Ans.: (a)
47. ..............•........ method of window object closes the current window.
(a) operu) (b) closet) . (c) blurt) (dy' alertt)
Ans.: (b)
4·8. .. . method of window object removes focus from the current window.
(a) printt) (b) operu) (c) prompu) (d) blurt)
Ans.: (d)
TPS InformationTechnology (Science) 3-23 Advanced JavaScript
109. . ; method of Array object joins two or more arrays, and returns a copy of the
joined arrays.
(a) findj) (b) concan) (c) indexOfO (d) popt)
Ans.: (b)
110. . method of Array object copies array elements within the array, to and from
specific positions.
(a) findO· (b) search() (c) copyWithin() (d) indexOf()
Ans.: (c)
TPS Information Technology (Science) 3-29 Advanced JavaScript
111. . method of Array object returns the value of the first element in an array
that satisfies a test in testing.
(a)concatO (b) popt) (c) pushO' (d) findt)
Ans.: (d)
112. . method of Array object calls a function for each array element.
(a) concan) (b) forEachO (c)popO (d) pushf)
Ans.: (b)
113. . method of Array object search the array for an element and returns its
position. .
(a) concatt) (b) forEachO (c) indexOfO (d) popt)
Ans.: (c)
114. . method of Array object checks whether an objectis an array.
(a) findf) (b) popt) (c) forEachO (d) isArrayO
Ans.: (d)
115. . method of Array object removes the last element of an array and returns that
element.
(a) popO (b) puslu) (c) findt) (d) reverser)
- Ans.: (a)
116; method of Array object add new elements to the end of an array, and
returns the new length.
(a) popO (b) pusht) (c) sortt) (d) findt)
Ans.: (b)
117. . method of Array object reverses the order of the elements in an array.
(a) findt) (b) isArrayO (c) popt) (d) reverser)
Ans. : (d)
118. . method of Array object sorts the elements of an array.
(a) c6ncatO(b) sortt) (c) popt) (d) find 0
Ans.: (b)
-
3.1 Introduction
1. Features of JavaScript are : .
(a) It needs special software
.(b) JavaScript is light weight scripting language'
(c) Cannot create new function
(d) It is object oriented scripting
Ans. : (b), (d)
· TPS Information Technology (Science) 3-30 Advanced JavaScript
3.1 Introduction
1. Features ofJavaScript are .
(a) Need of special software (b) It can handle date and time effectively
(c) It cannot create new functions (d) It can not create new furictions
(e) It supports event based programming
(f) It is platform dependent scripting language
Ans. : (b), (c), (e)
2. Server Side Scripting .
(a) It is used as front end •
(b) Doesnot need any server interactions
. . J •
I .~~_~.4_Java~c!ipt _Events .
10. Select three correct event handlers in JavaScript .
(a) close (b) open '(c) onsubmit (d)onchange
(e) onunload (f) write
Ans. : (c), (4),(e)
(I)
A B
(1) Head - (a)- Returns title of the document
(2) Title (b) Returns <body> <img> elements
(3) URL (c) Returns URL of document
(4) Body,img (d) Returns <head> element of the document
(e) Writes expression
Ans.: (1) - (d), (2) - (a), (3) - (c), (4) -(b)
(II)
A B
(1) OOM (a) Defines logical structure of document
(2) writet) (b) Client Side Script
(3) writelnO (c) In which id property is used to find element
(4) getElementByldO (d) Writes JavaScript Code
(e) Writes JavaScript code to a document
after each statement
' .
by adding new line
-~-. - --- - --- -_ ... _-------- -- -- - - - -
A B
(1) Location (a) Sets name of the window
(2) Document (b) Returns Boolean value indicating whether a window
(3) Status (c) Returns document object of window
(4) Closed (d) Returns the text in the status bar of a window
--"-
(5) --
Name--
(e) Returnsthe location object for the window -
Ans. : (1) - (e), (2) - (e), (3) - (d); (4) '- (b);(5),- (a)
(IV)
A B
(1) Window object (a) Displays dialog box containing message with ok and
cancel. button
(2) alertf) (b) Opens the new window
(3) confirmt) (c) Displays dialog box to get input from the user
(4) promptO (d) Parent object of all 'other objects
(5)
-
opent) (e) Displays box with ok button
Ans. : (1) - (d), (2) - (e), (3) - (a), (4) - (c), (5) - (b)
TPS Information Technology (Science) 3-37 Advanced JavaScript,
(V)
i
A B I
I
(2) On focus (b) Occurs when user selects some text of an element
(4) On select (d) Occurs when user leaves field or losses focus of an element
A B
I
(1) Onsubmit (a) . Occurs when user clicks on reset button
(3) Onload (c) Occurs when document page has been unloaded or closes
(I)
.A.
-.
B
(1) Length (a) Returns the character at the specified position
(2) chartAtO (b) Returns the index of last occurrence of specified character in
given story
(3) indexOfO (c) Returns the characters you specified
(4) lastIndexOfO (d) Returns the index of the first occurrence of the character
(5) substrt) (e) Returns a number of character in a string
- - _. - - - - -- -- _ .. -- --_. - -- - -
Ans. : (1) - (e), (2) - (a), (3) - (d), (4) - (b), (5) - (c)
(II)
A B
(1) substringt) (a) Returns the characters you specified
(2) trimO (b) Converts a string into lower case
(3) toLowerCaseO (c) Returns length of string
-
(4) to UpperCaseO (d) Converts the string into Upper case
(e) Removes white spaces from both sides of a string
_
----_. --- .. -- _-
--_ ..- ----- -- ~-- -----_ ..
(1) absO (a) Returns next integer greater than or equal to a given number -,
(2) cbrtO (b) Returns the highest valued number in a list of NUmbers
(3) ceilt) (c) Returns the absolute value of a number
(4) floorO (d) .Returns the cube root of a number
(5) max() (e) Returns next integer less than or equal to a given. number
Ans, : (1) - (c),(2) - (d), (3) -(a), (4) -(e), (5) - (b)
(IV)
A B
-
(1) mirif) (a) Returns the base to the exponent power
(2) powO (b) Returns the square root of a number
(3) randomt) (c) Returns the cube root of a number
(4) sqrtO (d) Returns a random number between 0 and 1
(e) Returns the lowest valued number in a list of number'
--
(V)
A B
(1) getbateO (a) Returns the year
(2) getDayO (b) Returns the Time
(3) getFullYearO (c) Returns the day of the week
(4) getHoursO (d) Returns the hour
(e) Returns the day of the month
Ans. : (1) - (e), (2)- (c), (3) - (a), (4) - (d)
(VI)
I
A B I
A B
(1) setlvlinutesl) (a) Sets the month of a date object
(2) setMonthO (b) Set a date to a specified number of miliseconds
(3) setSecondsO (c) Returns day of the week
(4) set'I'imer) (d) Set the minutes of a date object
(e) Sets the seconds of a date object
Ans. : (1) - (d), (2) - (a), (3) - (e), (4) - (b)
TPS InformationTechnology (Science) 3-40 Advanced JavaScript
(IX)
A B
(1) MIN_VALUE (a) Represent not a Number value
(2) MAX_VALUE (b) Determines whether the given value is a Integer
(3) NaN (c) Returns the largest minimum value
(4) isInteger( ) (d) Converts string into Floating point number
(e) Returns the largest max~urn ~~ue
Ans. : (1) - (c), (2) - (e), (3) - (a), (4) - (b)
(X)
A B
(1) parseFl6atO (a) Used to create date and time values
(2) parselntl) (b) Returns the string that represents a number with exact digits
after a decimal point
(3) isFixed() (c) Converts a given string into a floating point number
(4) Date object (d) Converts the given string into integer number
Ans. : (1) - (e), (2) - (d), (3) - (b), (4) - (a)
(IX)
A B
(1) concatt) (a) Returns the value of the first element in an array that satisfies a
test .
(2) copyWithinO (b) Search the array for an element and return its position
(3) findt) (c) Calls a function for each array element
.(4) forEachO (d) Joint two or more arrays and returns a copy of joined array
(5) indexOfO (e) Copies array elements within the array, to and from specified
positions.
- -- -
Ans. : (1) .;..(d), (2) - (e), (3) - (a), (4) - (e), (5) - (b)
(XII)
A B
(1) isArrayO (a) Adds a new elements to the end of an array, and returns the new
length
(2) popt) (b) Sorts the elements of an array
(3) PushO (c) Reverse the order of the elements in an array
(4) reverset) (d) Checks whether an object is an array
(5) sort( ) (e) Removes the last element of an array, and returns the element
--- _ -- - - .. - -- -- --- -
Ans. : (1) - (d), (2) - (e), (3) -(a), (4) - (c), (5) - (b)
TPS Information Technology (Science) 3-41 Advanced JavaScript
Switch Case:
The switch statement is used to perform different actions based on different
conditions, .
Syntax
switch( expression)
case x:
/ / code- block
break;
case y:
/ / code block
break;
default:
/l code block
break;
case 2:
alert("Welcome");
break;
case 3:
alert("How r u");
break;
TPS Information Technology (Science) 3-42 Advanced JavaScript
.~--------------------------------------~
case 4:
alert("Take care Good bye"):
break;
default:
alert("Invalid choice");
</script>
</html>
Coding:
x
.... ,
C
.
(i) File· i .E:/Xll%20SG%20IT%21L. * -e
This page says
Welcome
var i;
for(i=l;i<=lO;i++ )
document.writeti+'<br>"):
} .
</script>
</html>
Output:
1
2
3
4
5
6
7
8
9
10'
Note: "language" attribute of <script> tag is replace by "type" attribute in all programs
as it is standardised.
> . While Loop: The while loop loops through a block of code as long as a specified
condition is true.
Syntax :
Initialization;
while (condition)
Enter a Number
<input type="number" nam~~It1"><br><br>
<input type="button" name="bl" value="Odd Even" onClick=levenO">
</form> '
</body>
<script type="text/javascript">
function event)
{
vara;
a=frml.t1.value;
if(a%7==O)
alert("Number is even");
else
alert("Number is odd");
}
</script>
</htm1>
'Output :,
> Do while Loop : The do/while loop is a variant of the while loop. This loop will
execute the code block once, before checking if the condition is true, then it will
repeat the loop as long as the condition is true.
Syntax:
Initialization;
do {
/ / code block to be executed
}
while (condition);
TPS Information Technology (Science) 3-45 Advanced JavaScript
docurnent.writefi+l'<br>"):
i++;
while(i<=lO);
</script>
</html>
Output:
2
3
4
.5
6
7
8
9'
10
Coding:
<html>
, <body>
<form name="frml">
Enter a Number
,-
TPS Information Technology (Science) 3-46 Advanced JavaScript
onClick="prirneO">
</form>
</body>
<script type="text/javascript">
function primer)
var i,a,p;
a=parseInt( frml. tl. value);
.p=l;
for(i=2;i<a;i++ )
if(a%i===O)
p=O;
break;
if(p==l)
alert("Number is Prime number");
else
alert("Number is not a Prime number");
</script:>
</htrnl>
Output:
/
TPS Information Technology (Science) 3-47 Advanced JavaScript
var s=="<h2style=color-red>":
var t="DocumentObject Model";
var c=" </h2>";
document.getElementByld('para').
TPS Information Technology (Science) 3-48 Advanced JavaScript
innerHTML=s+t+c;
</script>
<body>
<p id="para">Welcome to mywebsite</p>
<form>
<input type="button" nameeb.l" value="Change Font" onClick="dorit()">
</form>
</body>
</html>
Output:
Before Button dick After Button Click
WelcO!lle'to my website
~ Window Object : Window object is the parent object of all other objects. It
represents an open window in a browser. An object of a window is created
automatically by the browser. Folfowing are the methods and properties of
Window objet ..
Property Description
name Sets or returns the name of a window.
location Returns the Location object for the window.
document. Returns the Document object for the window.
status Sets or returns the text in the status bar of a window.
closed Returns a Boolean value indicating whether a window has' been
closed or not.
TPS Information Technology (Science) .3-49 Advanced JavaScript
Method Description
alertt) Displays the alert box containing message with ok button.
confirmt) Displays the confirm dialog box containing message with ok and
cancel button.
promptO Displays a dialog box to get input from the user.
operu) Opens the new window.
closet) Closes the current window.
blurt) Removes focus from the current window.
focusO Sets focus to the current window.
prinn) Prints the content of current window.
set'I'imeoutt) Calls a function or evaluates an expression after a specified number
. of milliseconds .
- - .. -.~-.~- ---_ .. _ ... _--_ _-
.. - - -- ----
••
.( a ,,;::;:;1
¥DIJ[Noam£'IsBum
document.bgColore'tred":
window.setTimeout("color20",4000);
function color20 .
document.bgColor="green";
window.setTimeout("color30",4000);
TPS Information Technology (Science) 3-51 Advanced J avaScript
.function color30
documen.t.bgColor="blue";
window:setTimeout("color40",4000);
function color40
document.bgColor="yellow";
window.setTimeout("colorlO",4000);
.}
</script>
<body>
<form name="frml ">
<center>
<input type=''button'' name="bl" value="Change Colors"
onMouseOver="colorlO">
</center>
</form>
</body>
</htmi>
Output:
'" cO~Of,htm~
TPS Information.Technology (Science) 3-52 Advanced JavaScript
~ Events are the actions done by user that occurs on the web page. Following are
some of the events used with form objects.
Event handler Description
onblur It occurs when user leaves field or losses focus of an element.
onfocus It occurs when an element gets focus.
onchange It occurs when user changes content of an element. or selects
dropdown value. E.g .. for textbox, password, select box, textarea
etc.
onselect It occurs when user selects some text of an element.
onsubmit It occurs when user clicks submit button.
onreset It occurs when user clicks reset button.
onload It occurs when page/image has been loaded.
onunload It occurs when document/page has been unloaded or closes.
----
Method Description
char.Att): Returns the character at the specified position (in Number).
indexOfO Returns the index of the first occurence of specified character
in given string, or -1 if it never occurs, so with that index you
can determine if the string contains the specified character.
lastIndexOfO Returns the index of the last occurrence of specified character
in given string.
substrO Returns the characters you specified: (14,7) returns 7
characters, from the 14th character.
substringO Returns the characters you specified: (7,14) returns all
characters between the 7th and the 14th.
trimf) The trimt) method removes whitespace from both sides of
a String
toLowerCaseO Converts a string to lower case
toUpperCaseO Converts a string to upper case - -
Output: x
". 5tring,ht'11i
.~ "'" C (DFiie
Length of String is 26
First oceurance of 0 is 5
Last occurance of 0 is 21
Substring is nfor
Math Object :
The math object includes mathematical constants and functions. Following table
consists list of Math objects. .
Method Description
absO Returnsthe absolute value of a number:
cbrt(x) Returns the cube root of a number.
ceil(x) Returns the· next integer greater than or equal to a given
number (rounding up).
floor(x) Returns the next integer less than or equal to a given number
(rounding down).
max(x, y, ...) Returns the highest-valued number in a list of numbers.
min(x, y, ...) Returns the lowest-valued number in a list of numbers.
pow(x, y) Returns the base to the exponent power, that is, xy.
random(x) Returns a random number between 0 and 1 (including 0, but not 1).
sqrt(x) Returns the square root of a number,
- .. ------------------- - ---
TPS Information Technology (Science) 3-55 Advanced JavaScript
<t,. ") C
Value of X is -25.667
ValueofY is 10
PO'IV"et is 658)948890000001
. Date Object:
Date object is used to create date and time values. It is created using new keyword.
Method Description
getDateO Returns the day of the month (from 1-31)
getDayO Returns the day of the week (from 0-6)
getFullYearO Returns the year (four digits).
getHoursO Returns the hour (from 0-23).
getMinutesO Returns the minutes (from 0-59).
getMonthO Returns the month (from 0-11).
getSecondsO Returns the seconds (from 0-59).
getTimeO Returns the number of milliseconds since midnight jan I, 1970.
nowO Returns the number of milliseconds since midnightIan I, 1970.
setDateO Sets the day of the month of a date object.
setFullYearO Sets the full year of a date object.
setHoursO Sets the hours of a date object.
setMinutesO Set the minutes of a date object.
setMonthO Sets the month of a date object.
setSecondsO Sets the seconds of a date object.
setTimeO Sets a date to a specified number of milliseconds after Ibefore
Ian I, 1970.
~ Program to make use of various methods of date object.
-Coding:
<html>
<script type="textljavascript">
var d=new Datet):
document.wntet'xbrc-cbr» Date is "+d);
document.writef'xbrc-cbr» Day of the month is "+d.getDate());
document.write(,,<br><br> Day of the week is "+d.getDay());
document.write(,,<br><br> Year is "+d.getFullYear(»;
document.writet''cbrc-cbr» Month is "+d.getMonth());
document.writef''cbrc-cbr» Hours "+d.getHoursO);
document. write(" <br> <br> Minutes "s-d.getlvllnutesu):
document.write(" <br> <br> Seconds" +d.getSeconds());
TPS Information Technology (Science) 3-57 Advanced JavaScript
</html>
Output:
'" date,htmi
~" -); C
Year is 2020
Month is 3 '
\
Hours 14
Minutes 19
Second. 12
Number Object:
It helps in working with numbers. Property and Methods of number object are:
Property Description
MIN_VALUE Returns the largest minimum value.
¥AX_VALUE Returns the largest maximum value.
NaN It represents 'Not a Number' value.
Method Description
isIntegerO It determines whether the given value is a Integer
parseFloatO It converts the given string into a floating point number.
parselntt) It converts the given string into a integer number.
isFixedO It returns the string that represents a number with exact-digits after
a decimal point.
-- ~ _ ~- -- ... --- ----'"-- .. -.-~--.~ ..~. ~ --- -
Value of X is L7976931348623157e+308
Is integer false
Array Object:
An arrayis an object that can store a collection of items. Arrays are used' to store
multiple values in single variable. Array is a special variable which can hold more
than one values at a time. Property and Methods of array object are:
Property Description
Index The property represents the zero-based index of the match in the
string
Length Reflect number of elements in array. !
TPS Information Technology (Science) 3-59 Advanced JavaScript
Method Description
concat() Joins two or more arrays, and returns a copy of the joined arrays
copyWithinO Copies array elements within the array, to and from specied
.positions.
findt) Returns the value of the first element in an array that satisfies a test
in testing.
forEachO Calls a function for each array element.
indexOfO Search the array for an element and returns its position.
isArrayO Checks whether an object is an array.
popt) .Removes the last element ofan array, and returns that element.
pusht) Adds new elements to the end of an array, and returns the new
length.
reverser) Reverses the order of the elements in an array.
sortt) Sorts the elements of an.array.
_ .. _ .. -_ .. -- --- --- -- -- - - -
Red"Gl."een.Blue:-Orange,Pirik." Yeljow
Red .•Green.Blue"Orange"Pink
3.1 Introduction
1. Explain the features of JavaScript.
Ans.:
• JavaScript is light weight scripting language.
• No need of special software to run JavaScript Programs
• JavaScript is object oriented scripting language
• It can handle. date and time very effectively.
• It is case sensitive language.
2. Explain difference between Client Side and Server Side Scripting.
Ans.: Client Side Scripting:
(a) It is used at frontend which users can see from the browser.
(b) Client side scripting does not need any server interaction.
(c) Client Side scripting language involves languages such asHTML5, JavaScript etc.
(d) Client side scripting is used for validation purpose.
TPS Information Technology (Science) 3-61 Advanced JavaScript
case x:
II code block
break;
case y:
II code block
break;
default:
II code block
code to be executed
TPS Information Technology (Science) 3-62 Advanced JavaScript
code to be executed
Method Description
writet) Writes HTML expressions or JavaScript code to a document.
writelnO Same as write(), but adds a newline character after each
statement.
getElementByldO There are many ways of accessing form elements, of which the
easiest is by getElementByldO method. In which id property is
-used to find an element.
10. Explain alert, prompt and confirm methods of Window Object.
Ans. .
alertf) Displays the alert box containing message with ok button.
confirmf) Displays the confirm dialog box containing message with ok and cancel
button.
promptO Displays a dialog box to get input from the user.
concan) Joins two-or more arrays, and returns a copy of the joined arrays
indexOfO Search the array for an element and returns its position.
popO ,
Removes the last element of an array, and returns that element.
pusht) Adds new elements to the end of an array, and returns the new length.
__
reverset)
.. ~_.__ ____ -I
Reverses the order of the elements in an array,
_____ . __ , ___ _.. _
TPS Information Technology (Science) 3-65 Advanced JavaScript
var a,b,c,r;
a=parseInt(frm1.t1.value);
b=parseInt(frm1.t2.value);
c=parseInt(frm1.t3.value);
switch(c)
case 1:
r=a+b;
alert("Addition is "+r);
break;
case 2:
r=a-b;
alert("Substraction is "+r);
break;
case 3:
r=a*b;
alert("Multiplication is "+r);
.break;
case 4:
r=a/b;
alert("Division is "+r);
break;
default:
alert("Invalid choice ");
</script>
</html>·
TPS Information Technology (Science) 3-67 Advanced JavaScript
</script>
</html>
4. Program to display Evennumbers from 25to 50.
Coding:
<html>
<script type='text/javascript">
var i;
document.writef<brc-Even Numbers from 25-50 are <br>");
,.,
for(i=26·i<=50·i=i+2)
docnment.writef'Yt'x-i):
</script>
</html>
5. Program to print Multiplication table of the entered number:
Coding:
<html>
<script type="text/javascript">
var n,i,m;
n=prompt("Enter a Number");
document;write("<br>Multiplication Table is <br>"):
for(i=I;i<=lO;i++ )
TPS Information Technology (Science) 3~68 Advanced JavaScript
m=rrti:
document.write(n+" X H+i+" = "+m+"<br><br>n);
</script>
</html>
6. Programto print factorial of a number, accept number from the user.
Coding:
<html>
<script type='fext/jevascript''>
var n.i.f=I:
n=promptr'Enter a Number"):
for(i=n;i>=l;i-- )
f=f*i;
J
document.writef'<brc-Factorial is "+f);
</script>
</htm1>
</script>
</html>
3. Find and display factorial of given number.
Coding:
<html>
<script type="text/javascript">
var n=4,i,f=1;
TPS Information Technology (Science) 3-72 Advanced JavaScript
,for(i=n;i>=l;i--)
{
f=f>ti;
}
document.writef'<brc-Factorial of 4 is n+f);
</script>
</html>
4. Accept any string from user and count and display number of vowels occurs in it.
Coding:
<html>
<script type=tltext/javascripttl>
var n,i,ch,cnt=O;
n=prompt(tlEnter a String"):
for(i=O;i <n.length;i ++ )
{
ch=n.charAt(i);
if(ch=='a' I I ch=='A' I Ich=='e' I I ch=='E' I Ich=='i' I I ch=='i' I I
A B
1. cellO (a) Writes HTML expression or JavaScript code to a document.
2. floorO (b) Sets focus to current window.
3: writeO (c) Removes white ~paees from both sides of string.
4; .... focusf) (d) Returns next integer greater than or equal to given number.
5. trimO (e) Returns the next integer less than or equal to given number,
Ans.: 1. - d, 2. - e, 3. - a, 4. - b, . 5. ~c
DDD