Вы находитесь на странице: 1из 6

CS114 JavaScript Math Object and its methods

1.0 JavaScript Math Object and its methods

• The math object provides a set of methods for manipulating


numbers and performing basic mathematic computations.

• This includes methods for random number generation, exponential


functions and comparison functions.

• Some common methods are found in the table below.

2.0 A sample program

8-1
JavaScript Math Object and its methods CS114

3.0 JavaScript Date Object and its methods

• The Date object consists of several methods that will allow you to
determine the current time and date and perform calculations
between dates or times.

• You must create a date object with the keyword new before you
can manipulate date object.

• To create a Date object :

nameOfDateObject = new Date( );

• Example

today = new Date( );

8-2
CS114 JavaScript Math Object and its methods

4.0 JavaScript String Object and its methods

• A string consists of a sequence of characters.

• JavaScript has a list of methods that help manipulate a given


string.

• The general syntax is

o stringName.property

o stringName.methodName(parameters)

where stringName can be a string variable or constant

Method Description

length The length property of the string object


returns the number of characters in a string.

Ex: “Hello JavaScript”.length returns 16

bold( ) Emphasizes the text to be displayed in bold.


This method is equivalent to the HTML <B>…</B> tag.

8-3
JavaScript Math Object and its methods CS114

italics( ) Emphasizes the text to be displayed in


italics. This method is equivalent to the HTML <I>…</I> tag.

strike( ) Causes the text to be displayed as struck


out text. This method is equivalent to the HTML <Strike>
tag.

fontcolor( ) Causes the string to be displayed as the


specified colour.
Ex: var text=“Good Morning”
document.write(text.fontcolor(“red”));

fontsize( ) Changes the size of the text.


Ex: document.write (text.fontsize(3));

toLowerCase( ) This method converts the given given


string to lower case.
Ex: var text=“GOOD MORNING”;
document.write(text.toLowerCase( ) );

toUpperCase( ) This method converts the given string to


upper case.

Ex: var text=“good morning”;

document.write(text.toUpperCase( ) );

charAt( ) Returns the character at the specified


index. haracters in a string are indexed from the left to
right. Returns nothing if the specified position is greater
than the length of the string.
Ex : var text=“I Like JavaScript”;
document.write(text.charAt(0));
// I
document.write(text.charAt(1)); //

8-4
CS114 JavaScript Math Object and its methods

document.write(text.charAt(2)); //

substring(beginIndex,endIndex) Returns a substring of


the string specified by the beginIndex and endIndex.
Ex : text=“Informatics Computer School”;

document.write(text.substring(12,19));
// will display Computer

document.write(text.substring(21,26));
// will display School

indexOf(text,Index ) Returns the position number in


a string of the first character in the text argument. The Index
is an optional value that represents the position from
which to begin the search.

Ex: var text="Good Morning";

document.write(text.indexOf("M"));
// will display 5

8-5
JavaScript Math Object and its methods CS114

5.0 Bibliography

• JavaScript (Introductory) By Dan Gosselin

8-6

Вам также может понравиться