Professional Documents
Culture Documents
JavaScript: Built-in-Objects
2
Math Object
Me th o d De sc rip tio n Exa m p le
abs( x ) absolute value of x abs( 7.2 ) is 7.2
abs( 0.0 ) is 0.0
abs( -5.6 ) is 5.6
ceil( x ) rounds x to the smallest integer not less than ceil( 9.2 ) is 10.0
x ceil( -9.8 ) is -9.0
cos( x ) trigonometric cosine of x cos( 0.0 ) is 1.0
(x in radians)
exp( x ) exponential method ex exp( 1.0 ) is 2.71828
exp( 2.0 ) is 7.38906
floor( x ) rounds x to the largest integer not greater floor( 9.2 ) is 9.0
than x floor( -9.8 ) is -10.0
log( x ) natural logarithm of x (base e) log( 2.718282 ) is 1.0
log( 7.389056 ) is 2.0
max( x, y ) larger value of x and y max( 2.3, 12.7 ) is 12.7
max( -2.3, -12.7 ) is -2.3
min( x, y ) smaller value of x and y min( 2.3, 12.7 ) is 2.3
min( -2.3, -12.7 ) is -12.7
pow( x, y ) x raised to power y (xy) pow( 2.0, 7.0 ) is 128.0
pow( 9.0, .5 ) is 3.0
round( x ) rounds x to the closest integer round( 9.75 ) is 10
round( 9.25 ) is 9
sin( x ) trigonometric sine of x sin( 0.0 ) is 0.0
(x in radians)
sqrt( x ) square root of x sqrt( 900.0 ) is 30.0
sqrt( 9.0 ) is 3.0
tan( x ) trigonometric tangent of x tan( 0.0 ) is 0.0
(x in radians)
3
Math Object
lastIndexOf( Searches for the last occurrence of substring starting from position index and
substring, index ) searching toward the beginning of the string that invokes the method. The method returns
the starting index of substring in the source string or –1 if substring is not found. If
the index argument is not provided, the method begins searching from end of the source
string.
slice( start, end ) Returns a string containing the portion of the string from index start through index
end. If the end index is not specified, the method returns a string from the start index
to the end of the source string. A negative end index specifies an offset from the end of
the string starting from a position one past the end of the last character (so, –1 indicates
the last character position in the string).
split( string ) Splits the source string into an array of strings (tokens) where its string argument
specifies the delimiter (i.e., the characters that indicate the end of each token in the
5
Me th o d De sc rip tio n
substr( Returns a string containing length characters starting from index start in the source
start, length ) string. If length is not specified, a string containing characters from start to the end
of the source string is returned.
substring( Returns a string containing the characters from index start up to but not including
start, end ) index end in the source string.
toLowerCase() Returns a string in which all uppercase letters are converted to lowercase letters. Non-
letter characters are not changed.
toUpperCase() Returns a string in which all lowercase letters are converted to uppercase letters. Non-
letter characters are not changed.
Program Output
8
1 <?xml version = "1.0"?> Outline
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
3 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
4
5 <!-- Fig. 12.5: SearchingStrings.html --> SearchingStrings
6 <!-- Searching Strings --> .html
7
8 <html xmlns = "http://www.w3.org/1999/xhtml">
9 <head>
10 <title>
11 Method
Searching Strings with indexOf indexOf determines the first
and lastIndexOf
12 </title> occurrence in the string letters of the string
13
14 <script type = "text/javascript">
searchForm.inputVal.value.
15 <!--
16 var letters = "abcdefghijklmnopqrstuvwxyzabcdefghijklm";
17
18 function buttonPressed()
19 { Method lastIndexOf determines the location of
20 searchForm.first.value = the last occurrence in letters of the string in text
21 letters.indexOf( searchForm.inputVal.value );
22 searchForm.last.value = field inputVal.
23 letters.lastIndexOf( searchForm.inputVal.value );
24 searchForm.first12.value =
25 letters.indexOf( searchForm.inputVal.value, 12 );
26 searchForm.last12.value =
27 letters.lastIndexOf(
28 searchForm.inputVal.value, 12 );
29 }
30 // -->
31 </script>
32
33 </head>
9
34 <body> Outline
35 <form name = "searchForm" action = "">
36 <h1>The string to search is:<br />
37 abcdefghijklmnopqrstuvwxyzabcdefghijklm</h1>
38 <p>Enter substring to search for SearchingStrings
39 <input name = "inputVal" type = "text" /> .html
40 <input name = "search" type = "button" value = "Search"
41 onclick = "buttonPressed()" /><br /></p>
42
43 <p>First occurrence located at index
44 <input name = "first" type = "text" size = "5" />
45 <br />Last occurrence located at index
46 <input name = "last" type = "text" size = "5" />
47 <br />First occurrence from index 12 located at index
48 <input name = "first12" type = "text" size = "5" />
49 <br />Last occurrence from index 12 located at index
50 <input name = "last12" type = "text" size = "5" /></p>
51 </form>
52 </body>
53 </html>
10
Outline
Program Output
11
1 <?xml version = "1.0"?> Outline
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
3 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
4
5 <!-- Fig. 12.6: SplitAndSubString.html --> SplitAndSubStrin
6 <!-- String Method split and substring --> g.html
7
8 Method split tokenizes
<html xmlns = "http://www.w3.org/1999/xhtml"> the contents of text
9 <head> field inputVal.
10 <title>String Method split and substring</title>
11
12 <script type = "text/javascript">
13 <!--
14 function splitButtonPressed()
15 {
16 var strings = myForm.inputVal.value.split( " " );
17 myForm.output.value = strings.join( "\n" );
18
19 myForm.outputSubstring.value =
20 The argument
myForm.inputVal.value.substring( 0,to10
method
); split is the delimiter string.
21 }
22 // -->
23 </script>
24 </head>
25 Method subString obtains a string containing the first 10 characters
26 <body> of the string the user input in text field inputVal.
27 <form name = "myForm" action = "">
28 <p>Enter a sentence to split into words<br />
29 <input name = "inputVal" type = "text" size = "40" />
30 <input name = "splitButton" type = "button" value =
31 "Split" onclick = "splitButtonPressed()" /></p>
32
33 <p>The sentence split into words is<br />
34 <textarea name = "output" rows = "8" cols = "34">
35 </textarea></p>
12
36 Outline
37 <p>The first 10 characters of the input string are
38 <input name = "outputSubstring" type = "text"
39 size = "15" /></p>
40 </form> SplitAndSubStrin
41 </body> g.html
42 </html>
Program Output
13
1 <?xml version = "1.0"?> Outline
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
3 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
4
5 <!-- Fig. 12.7: MarkupMethods.html --> MarkupMethods.ht
6 <!-- XHTML markup methods of the String object --> ml
7
8 <html xmlns = "http://www.w3.org/1999/xhtml">
9 <head>
10Method strike displaysMarkup
<title>XHTML Methods of the StringMethod
text with anchor marks up the text as an anchor.
Object</title>
Method
11a line subit.creates subscript
through
12Method
text. sup creates
<script superscript
type = "text/javascript"> Method blink makes the string blink in the
13
text. <!-- Web page.
14 The link method creates a = "This is an anchor", Method fixed displays txt in a fixed-
var anchorText
15 hyperlink. blinkText = "This is blinking text", width font.
16 fixedText = "This is monospaced text",
17 linkText = "Click here to go to anchorText",
18 strikeText = "This is strike out text",
19 subText = "subscript",
20 supText = "superscript";
21
22 document.writeln( anchorText.anchor( "top" ) );
23 document.writeln( "<br />" + blinkText.blink() );
24 document.writeln( "<br />" + fixedText.fixed() );
25 document.writeln( "<br />" + strikeText.strike() );
26 document.writeln(
27 "<br />This is text with a " + subText.sub() );
28 document.writeln(
29 "<br />This is text with a " + supText.sup() );
30 document.writeln(
31 "<br />" + linkText.link( "#top" ) );
32 // -->
14
33 </script> Outline
34
35 </head><body></body>
36 </html>
MarkupMethods.ht
ml
Program Output
15
Me th o d De sc rip tio n
toString() Returns the string “true” if the value of the Boolean object is true; otherwise, returns
the string “false.”
valueOf() Returns the value true if the Boolean object is true; otherwise, returns false.
16
Number.MAX_VALUE This property represents the largest value that can be stored in a JavaScript
program—approximately 1.79E+308
Number.MIN_VALUE This property represents the smallest value that can be stored in a JavaScript
program—approximately
2.22E–308
Number.NaN This property represents not a number—a value returned from arithmetic
expressions that do not result in a number (e.g., the expression
parseInt("hello") cannot convert the string "hello" into a
number, so parseInt would return Number.NaN). To determine
whether a value is NaN, test the result with function isNaN which returns
true if the value is NaN; otherwise, it returns false.
Number.NEGATIVE_INFINITY This property represents a value less than
-Number.MAX_VALUE.
Number.POSITIVE_INFINITY This property represents a value greater than
Number.MAX_VALUE.
Additional Facts on JS
JavaScript Functions
• Recursive functions
– Recursion (function calling itself, either directly or
indirectly) is supported
JavaScript Functions
JavaScript: Arrays
21
Arrays
Name of array (Note
c[ 0 ] -45
that all elements of c[ 1 ] 6
this array have the
same name, c) c[ 2 ] 0
c[ 3 ] 72
c[ 4 ] 1543
c[ 5 ] -89
c[ 6 ] 0
c[ 7 ] 62
c[ 8 ] -3
c[ 9 ] 1
Position number (index
or subscript) of the c[ 10 ] 6453
element within array c
c[ 11 ] 78
Program Output
25
1 <?xml version = "1.0"?> Outline
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
3 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
4
5 <!-- Fig. 11.4: InitArray2.html --> InitArray2.html
6 <!-- Initializing an Array with a Declaration -->
7
8 <html xmlns = "http://www.w3.org/1999/xhtml">
9 <head>
10
Array integers1 is initialized using
<title>Initializing an Array with a Declaration</title>
an initializer list.
11
12 <script type = "text/javascript">
13 <!-- Two values are not supplied for integer2,
14 function start() which will be displayed as undefined.
15 {
16 // Initializer list specifies number of elements and
17 // value for each element.
18 var colors = new Array( "cyan", "magenta",
19 "yellow", "black" );
20 var integers1 = [ 2, 4, 6, 8 ];
21 var integers2 = [ 2, , , 8 ];
22
23 outputArray( "Array colors contains", colors );
24 outputArray( "Array integers1 contains", integers1 );
25 outputArray( "Array integers2 contains", integers2 );
26 }
27
26
28 // output "header" followed by a two-column table Outline
29 // containing subscripts and elements of "theArray"
30 function outputArray( header, theArray )
31 {
32 document.writeln( "<h2>" + header + "</h2>" ); InitArray2.html
33 document.writeln( "<table border = \"1\"" +
34 "width = \"100%\">" );
35 document.writeln( "<thead><th width = \"100\" " +
36 "align = \"left\">Subscript</th>" +
37 "<th align = \"left\">Value</th></thead><tbody>" );
38
39 for ( var i = 0; i < theArray.length; i++ )
40 document.writeln( "<tr><td>" + i + "</td><td>" +
41 theArray[ i ] + "</td></tr>" );
42
43 document.writeln( "</tbody></table>" );
44 }
45 // -->
46 </script>
47
48 </head><body onload = "start()"></body>
49 </html>
27
Outline
Program Output
28
1 <?xml version = "1.0"?> Outline
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
3 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
4
5 <!-- Fig. 11.5: SumArray.html --> SumArray.html
6 <!-- Summing Elements of an Array -->
7
8 <html xmlns = "http://www.w3.org/1999/xhtml">
9 <head>
10 <title>Sum the Elements of The for loop sums
an Array</title> the values contained in the 10-
11 element integer array called theArray.
12 <script type = "text/javascript">
13 <!--
14 function start()
15 {
16 var theArray = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ];
17 var total1 = 0, total2 = 0;
18
19 Variable element
for ( var i = 0; i < theArray.length; i++ ) is assigned a subscript
20 total1 += theArray[ i ];
21
in the range of 0 up to, but not including,
22 document.writeln( "Total theArray.length.
using subscripts: " + total1 );
23
24 for ( var element in theArray )
25 total2 += theArray[ element ];
26
27 document.writeln( "<br />Total using for/in: " +
28 total2 );
29 }
30 // -->
31 </script>
32
33 </head><body onload = "start()"></body>
34 </html>
29
Outline
Program Output
30
Multiple-Subscripted Arrays
Co lu m n 0 Co lu m n 1 Co lu m n 2 Co lu m n 3
Ro w 0 a[ 0 ][ 0 ] a[ 0 ][ 1 ] a[ 0 ][ 2 ] a[ 0 ][ 3 ]
Ro w 1 a[ 1 ][ 0 ] a[ 1 ][ 1 ] a[ 1 ][ 2 ] a[ 1 ][ 3 ]
Ro w 2 a[ 2 ][ 0 ] a[ 2 ][ 1 ] a[ 2 ][ 2 ] a[ 2 ][ 3 ]
Co lu mn su b sc rip t (o r in d e x)
Ro w su b sc rip t (o r in d e x)
Arra y n a m e
Fig. 11.11 Double-subscripted array with three rows and four columns.
31
1 <?xml version = "1.0"?> Outline
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
3 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
4
5 <!-- Fig. 11.12: InitArray3.html Array array1
--> provides six initializers inInitArray3.html
6 <!-- Initializing Multidimensional Arrays
two -->
sublists.
7
8 <html xmlns = "http://www.w3.org/1999/xhtml">
9 <head> Array array2 provides six initializers in
10 three sublists.
<title>Initializing Multidimensional Arrays</title>
11
12 <script type = "text/javascript">
13 <!--
14 function start()
15 {
16 var array1 = [ [ 1, 2, 3 ], // first row
17 [ 4, 5, 6 ] ]; // second row
18 var array2 = [ [ 1, 2 ], // first row
19 [ 3 ], // second row
20 [ 4, 5, 6 ] ]; // third row
21 Function outputArray displays each array’s
22 outputArray( "Valueselements in abyWeb
in array1 page.
row", array1 );
23 outputArray( "Values in array2 by row", array2 );
24 }
25
26 function outputArray( header, theArray )
27 {
28 document.writeln( "<h2>" + header + "</h2><tt>" );
29
32
30 for ( var i in theArray ) { Outline
31
32 for ( var j in theArray[ i ] )
33 document.write( theArray[ i ][ j ] + " " );
34 InitArray3.html
35 document.writeln( "<br />" );
36 }
37 Referencing the multidimensional
38 document.writeln( "</tt>" );
39 } array theArray.
40 // -->
41 </script>
42
43 </head><body onload = "start()"></body>
44 </html>
Program Output