Properties: The Math Object
Properties: The Math Object
Properties: The Math Object
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!
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:
<BLINK>This is BLINKING
text</BLINK>
Producing:
This is TT 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:
<STRIKE>This is STRIKEOUT
text</STRIKE>
Producing:
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
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.
Methods
Examples
<script type="text/javascript">
</script>
Output:
Below example returns the day of the week a specific past date falls on:
This example calculates the number of days passed since the Year 2000:
This example sets the Date object 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:
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