Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Properties: The Math Object

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 14

The Math object

Below lists all of Math's object's properties and methods:

Properties
Properties Description
E The constant of E, the base of natural logarithms.
LN2 The natural logarithm of 2.
LN10 The natural logarithm of 10.
LOG2E Base 2 logarithm of E.
LOG10E Base 10 logarithm of E.
PI Returns PI.
SQRT1_2 Square root of 1/2.
SQRT2 Square root of 2.

Methods
Methods Description
abs(x) Returns absolute value of x.
acos(x) Returns arc cosine of x in radians.
asin(x) Returns arc sine of x in radians.
atan(x) Returns arc tan of x in radians.
atan2(y, x) Counterclockwise angle between x axis and point (x,y).
ceil(x) Returns the smallest integer greater than or equal to x.
(round up).
cos(x) Returns cosine of x, where x is in radians.
exp(x) Returns ex
floor(x) Returns the largest integer less than or equal to x.
(round down)
log(x) Returns the natural logarithm (base E) of x.
max(a, b) Returns the larger of a and b.
min(a, b) Returns the lesser of a and b.
pow(x, y) Returns Xy
random() Returns a pseudorandom number between 0 and 1.
Example(s).
round(x) Rounds x up or down to the nearest integer. It rounds .5
up. Example(s).
sin(x) Returns the Sin of x, where x is in radians.
sqrt(x) Returns the square root of x.
tan(x) Returns the Tan of x, where x is in radians.

Let's have JavaScript solve some mathematical problems that have baffled mankind
for ages:

//calculate e5
Math.exp(5)
//calculate cos(2PI)
Math.cos(2*Math.PI)
The "with" statement

If you intend to invoke Math multiple times in your script, a good statement to
remember is "with." Using it you can omit the "Math." prefix for any subsequent
Math properties/methods:

with (Math){
var x= sin(3.5)
var y=tan(5)
var result=max(x,y)
}
And with that the tutorial comes to a wrap. Have fun crunching some numbers!

JavaScript String Object


Describes the javascript string object including its properties and
methods while providing code examples.

Properties
 length - The number of characters in the string.
 prototype - For creating more properties

Methods
 anchor(anchorName) - A string is displayed as an anchor with the name specified.
 big() - String is displayed in large format. (big tags)
 document.write("This is BIG text".big())
Is the same as:

<BIG>This is BIG text</BIG>


Producing:

This is BIG text

 blink() - String is displayed blinking. (blink tags)


 document.write("This is BLINKING
text".blink())
Is the same as:

<BLINK>This is BLINKING
text</BLINK>
Producing:

This is BLINKING text

 bold() - String is displayed in bold characters. (bold tags)


 document.write("This is BOLD
text".bold())
Is the same as:

<B>This is BOLD text</B>


Producing:

This is BOLD text

 charAt(index) - Returns a string containing the character at the specified location.


The example returns the character "r".
 myname = "George"
 myname.charAt(3)
 fixed() - A string is displayed in Teletype format (teletype tags).
 document.write("This is TT
text".fixed())
Is the same as:
<TT>This is TT text</TT>
Producing:

This is TT text

 fontcolor(color) - String is displayed in the specified color.


 document.write("This is RED
text".fontcolor("red"))
Is the same as:

<FONT color="red">This is RED


text</FONT>
Producing:

This is RED text

 fontsize(size) - String is displayed in the specified font from a value of 1 to 7.


 document.write("This is SIZE 5
text".fontsize(5))
Is the same as:

<FONT size="5">This is SIZE 5


text</FONT>
Producing:

This is SIZE 5 text


 indexOf(pattern) - Returns -1 if the value is not found and returns the index of the
first character of the first string matching the pattern in the string. The example
will return a value of 3.
 myname = "George"
 myname.indexOf("r")
 indexOf(pattern, index) - Returns -1 if the value is not found and returns the index
of the first character of the first string matching the pattern in the string.
Searching begins at the index value in the string.
 italics() - String is displayed using the italics tags.
 document.write("This is ITALICS
text".italics())
Is the same as:

<I>This is ITALICS text</I>


Producing:

This is ITALICS text

 lastIndexOf(pattern) - Returns -1 if the value is not found and returns the index of
the first character of the last string matching the pattern in the string.
 lastIndexOf(pattern, index) - Returns -1 if the value is not found and returns the
index of the first character of the last string matching the pattern in the string.
Searching begins at the index value in the string. The example will return a value
of 5.
 myname = "George"
 myname.lastIndexOf("e")
 link(href) - A string is displayed as a hypertext link.
 document.write("Computer
Technology Documentation
Project".link("http://ctdp.tripod.
com/"))
Is the same as:

<A
HREF="http://ctdp.tripod.com/">Com
puter Technology Documentation
Project</A>
Producing:

Computer Technology Documentation Project

 small() - String is displayed using the small tags.


 document.write("This is SMALL
text".small())
Is the same as:
<SMALL>This is SMALL text</SMALL>
Producing:

This is SMALL text

 split(separator) - Splits a string into substrings based on the separator character. In


the below example an array called splitValues is created. The splitValues[0] string
will be "Day" and the splitValues[0] string will be "Monday".
 nvpair = new String("Day=Monday")
 splitValues=nvpair.split("=")
 strike() - String is displayed using the strikeout tags.
 document.write("This is STRIKEOUT
text".strike())
Is the same as:

<STRIKE>This is STRIKEOUT
text</STRIKE>
Producing:

This is STRIKEOUT text

 sub() - String is displayed using the subscript tags.


 document.write("This is SUBSCRIPT
text".sub())
Is the same as:

<SUB>This is SUBSCRIPT text</SUB>


Producing:

This is SUBSCRIPT text

 substr(start, length) - Returns the string starting at the "start" index of the string
Continuing for the specified length of characters unless the end of the string is
found first. The example will return "org".
 mysite = "Comptechdoc.org"
 myname.substring(12, 3)
 substring(start, end) - Returns the string starting at the "start" index of the string
and ending at "end" index location, less one. The example will return "org".
 mysite = "Comptechdoc.org"
 myname.substring(12,15)
 sup() - String is displayed using the superscript tags.
 document.write("This is
SUPERSCRIPT text".sup())
Is the same as:

<SUP>This is SUPERSCRIPT
text</SUP>
Producing:
This is SUPERSCRIPT text

 toLowerCase() - Returns a copy of the string with all characters in lower case.
 toUpperCase() - Returns a copy of the string with all characters in upper case.

Date Object
Last updated: July 30th, 2004

There are fours ways of instantiating a date object:

new Date()
new Date(milliseconds)
new Date(dateString)
new Date(year, month, day, hours, minutes, seconds, milliseconds) //most
parameters here are optional. Not specifying causes 0 to be passed in.

Here are a few examples of instantiating a date:

today = new Date()


birthday = new Date("March 11, 1985 09:25:00")
birthday = new Date(85,2,11)
birthday = new Date(85,2,11,9,25,0)

Methods

Note: "[]" surrounding a parameter below means the parameter is optional.


Methods Description
getFullYear() Returns year in full 4 digit format (ie: 2004).
getYear() Returns the year, in 4 digit format or otherwise
depending on browser. Deprecated in favor of
getFullYear().
getMonth() Returns the month. (Range is 0-11)!
getDate() Returns the day of the month (Range is 1-31)
getDay() Returns the day of the week (Range is 0-6). 0=Sunday,
1=Monday, etc.
getHours() Returns the hour (Range is 0-23).
getMinutes() Returns the minutes. (Range is 0-59).
getSeconds() Returns the seconds. (Range is 0-59).
getMilliseconds() Returns the milliseconds. (Range is 0-999).
getTime() Returns the millisecond representation of the current
Date object. In the words, the number of milliseconds
between 1/1/1970 (GMT) and the current Date object.
getTimezoneOffset() Returns the offset (time difference) between Greenwich
Mean Time (GMT) and local time of Date object, in
minutes.
getUTCFullYear() Returns the full 4 digit year in Universal time.
getUTCMonth() Returns the month in Universal time.
getUTCDate() Returns the day of the month in Universal time.
getUTCDay() Returns the day of the week in Universal time.
getUTCHours() Returns the hour in Universal time.
getUTCMinutes() Returns the minutes in Universal time.
getUTCSeconds() Returns the seconds in Universal time.
getUTCMilliseconds() Returns the milliseconds in Universal time.
   
setFullYear(year, [month], Sets the year of the Date object. (year: 4 digit year).
[day])
setYear(year) Sets the year of the Date object. "year" can be two digits
(1900 is automatically added to it), or 4 digits.
Deprecated over setFullYear() above.
setMonth(month, [day]) Sets the month of the Date object. (month: 0-11)
setDate(day_of_month) Sets the day of the month of the Date object.
(day_of_month: 1-31).
setHours(hours, [minutes], Sets the hour of the Date object. (hours: 0-23).
[seconds], [millisec])
setMinutes(minutes, Sets the minutes of the Date object. (minutes: 0-59).
[seconds], [millisec])
setSeconds(seconds, Sets the seconds of the Date object. (seconds: 0-59).
[millisec])
setMilliseconds(milli) Sets the milliseconds field of the Date object. (milli: 0-
999)
setTime(milli) Sets the value of the Date object in terms of milliseconds
elapsed since 1/1/1970 GMT.
setUTCFullYear(year, Sets the year of the Date object in Universal time.
[month], [day])
setUTCMonth(month, Sets the month in Universal time.
[day])
setUTCDate(day_of_month Sets the day of the month in Universal time.
)
setUTCHours(hours, Sets the hours in Universal time.
[minutes], [seconds],
[millisec])
setUTCMinutes(minutes, Sets the minutes in Universal time.
[seconds], [millisec])
setUTCSeconds(seconds, Sets the seconds in Universal time.
[millisec])
setUTCMilliseconds(milli) Sets the milliseconds in Universal time.
   
toGMTString() Converts a date to a string, using the GMT conventions.
Deprecated in favor of toUTCString().
toLocaleString() Converts a date to a string, using current locale
conventions.
toLocaleDateString() Returns the date portion of the Date as a string, using
current locale conventions.
toLocaleTimeString() Returns the time portion of the Date as a string, using
current locale conventions.
toString() Converts a Date to human-readable string.
toUTCString() Converts a Date to human-readable string, in Universal
time.
parse(datestring) Returns the number of milliseconds in a date string since
1/1/1970. (datestring: a string containing the date/time
to be parsed).
UTC(year, month, [day], Returns the number of milliseconds in a date string since
[hours], [minutes], 1/1/1970, Universal time.
[seconds], [milli])
valueOf() Converts a Date to milliseconds. Same as getTime();.

Examples

Example 1- write out current date

To write out today's date for example, you would do:

<script type="text/javascript">

var mydate= new Date()


var theyear=mydate.getFullYear()
var themonth=mydate.getMonth()+1
var thetoday=mydate.getDate()

document.write("Today's date is: ")


document.write(theyear+"/"+themonth+"/"+thetoday)

</script>

Output:

Example 2- calculate day of week of a date

Below example returns the day of the week a specific past date falls on:

birthday = new Date(1978,2,11)


weekday = birthday.getDay()
alert(weekday) //alerts 6, or Saturday

Example 3- calculate number of days expired

This example calculates the number of days passed since the Year 2000:

var today=new Date()


var year2000=new Date(2000,0,1)
var diff=today-year2000 //unit is milliseconds
diff=Math.round(diff/1000/60/60/24) //contains days passed since Year
2000

Example 4- set date to be a future date

This example sets the Date object to be 3 days into the future:

var today=new Date()


today.setDate(today.getDate()+3) //today now is set to be 3 days into
the future

Boolean Object
Last updated: June 20th, 2004

The Boolean object is an object wrapper for a boolean (true or false) value. You can
explicitly define a Boolean via:

new Boolean([value])

1) Value- Initial value of the Boolean object. The value is converted to a boolean
value, if necessary. If value is not specified, 0, -0, null, false, NaN, undefined, or the
empty string (""), the object is set to false. All other values, including any object or
the string "false", create an object with a value of true.

For example:

var guess=new Boolean(false) //false value


var guess=new Boolean(0) //false value
var guess=new Boolean(true) //true value
var guess=new Boolean("whatever") //true value

Properties
Properties Description
constructor Specifies the function that created the object's prototype.
prototype Allows you to define properties on the Boolean that is shared by
all Boolean objects.
Methods
Methods Description
toString() Returns a string specifying the value of the Boolean, in this
case, "true" or "false."
valueOf() Returns the primitive value of a Boolean object.

Number Object
The Number object is an object wrapper for primitive numeric values. It contains some
useful methods, introduced in JavaScript 1.5.

Constructor:

new Number(value)
Number (value)

where "value" is the numeric value of the Number object to be created, or value to be
converted to a number.

Properties
Properties Description
MAX_VALUE The largest representable number in JavaScript.
MIN_VALUE The smallest representable number in JavaScript.
NaN "Not a number" value.
NEGATIVE_INFINITY Negative infinity, returned on overflow.
POSITIVE_INFINITY Infinity, returned on overflow.
prototype Prototype property, to add custom properties and methods to
this object.

Methods
Methods Description
toExponential(x) Returns a number in exponential notation (string format). "x"
should be a number between 0 and 20, dictating the number of
digits to include in the notation after the decimal place.
toFixed(x) Formats any number for "x" number of trailing decimals. The
number is rounded up, and "0"s are used after the decimal point
if needed to create the desired decimal length. Example(s)
toPrecision(x) Formats any number so it is of "x" length. Also called significant
digits. A decimal point and "0"s are used if needed to create the
desired length.

Note: All methods above are introduced in JavaScript 1.5, and only supported in IE6+
and NS6+.

Examples

toFixed(x)

var profits=2489.8237
profits.toFixed(3) //returns 2489.824 (round up)
profits.toFixed(2) //returns 2489.82

You might also like