Predefined Objects
Let us assume some of the most frequently utilized predefined objects provided in Javascript.
Math object
In most of the applications we have to perform calculations, whether this is accounting software or scientific software. Programmers are frequently typecast as good mathematicians. Every mathematician required a calculator sometimes, or in the particular case of JavaScript, the Math object. If we desire to calculate "2.5 to the power of 8" or "Sin0.9" in your script, then JavaScript's virtual calculator is what you desire. The Math object have a number of manipulating functions:
The Math object
Methods
|
Description
|
Math.abs(x)
|
Return absolute value of x
|
Math.acos(x)
|
Return arc cosine of x in radians
|
Math.asin(x)
|
Return arc sine of x in radians
|
Math.atan(x)
|
Return arc tan of x in radians
|
Math.atan2(x, y)
|
Counterclockwise angle among x axis & point (x,y)
|
Math.ceil(x)
|
Rounds a number up
|
Math.cos(x)
|
Trigonometric cosine of x (x in radians)
|
Math.exp(x)
|
Exponential method ex
|
Math.floor(x)
|
Rounds number down
|
Math.log(x)
|
Natural logarithm of x (base e)
|
Math.max(a, b)
|
Returns the larger of two values
|
Math.min(a, b)
|
Returns the smaller of two values
|
Math.pow(x, y)
|
Returns xy
|
Math.round(x)
|
Rounds x to the closest integer
|
Math.sin(x)
|
Trigonometric sine of x (x in radians)
|
Math.sqrt(x)
|
Square root of x
|
Math.tan(x)
|
Trigonometric tangent of x (x in radians)
|
Properties
|
Description
|
Math.E
|
Euler's constant ( ~ 2.718)
|
Math.LN10
|
Natural logarithm of 10 (~ 2.302)
|
Math.LN2
|
Natural logarithm of 2 (~ 0.693)
|
Math.LOG10E
|
Base 10 logarithm of Euler's constant (~ 0.0434)
|
Math.LOG2E
|
Base 2 logarithm of Euler's constant (~1.442)
|
Math.PI
|
The ratio of a circle's circumference to its diameter
(~3.141)
|
Math.SQRT1_2
|
Square root of 0.5 ( ~ 0.707)
|
Math.SQRT2
|
Square root of 2.0 ( ~ 1.414)
|
Let us encompass Javascript perform some mathematical calculations:
//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)
}