WbWizard's JS Book

Blue Line

Lesson 13: Math)

Today we will be discussing Math in JavaScript terms. The basis of any programming language can ultimately be broken down into a series of 1s and 0s. The patterns and flows created by these 1s and 0s are then manipulated in a language specific way using mathematics in some fashion. As webpage programmers (or application creators) we are not concerned with the underlying math that allows the browser's JavaScript interpreter to handle data, but rather how we can manipulate data of our choosing for use on top of the programming language as an application.

1) Arithmetic Operators
+ addition
sum=1+1; sum==2

- subtration and/or negation

diff=2-1; diff==2
* multiplication
multi=2*3; mulit==6

/ division
div=4/2; div==2

% modulo (finds the remainder after division)
mod=5%2; mod==1

++ increment (add one)
inc=0; inc++; inc==1

-- decrement (subtract one)
dec=0; dec--; dec==(-1)

() containers (JavaScript obeys algebraic rules)
func1=1+2*3; func1==7
func2=(1+2)*3; func2==9

2) Math Constants

Math.E is e, the base of the natural logarithm
Math.LN10 is the natural logarithm of 10
Math.LN2 is the natural logarithm of 2
Math.LOG10E is the base-10 logarithm of e
Math.LOG2E is the base-2 logarithm of e
Math.PI is the circular constant of pi
Math.SQRT1_2 is 1 divided by square root 2
Math.SQRT2 is the square root of 2

3) JavaScript Static Mathematic Functions

Math.abs(x); absolute value of x
Math.acos(x); inverse cosine of x (arc cosine)
Math.asin(x); arc sine of x
Math.atan2(x,y); computes angle from x axis
Math.ceil(x); rounds x up to next integer
Math.cos(x); the cosine of x
Math.exp(x); set e to an the x power
Math.floor(x); rounds down to previous integer
Math.log(x); the natural logarithm of x
Math.max(a,b); returns the larger of a or b
Math.min(a,b); returns the smaller of a or b
Math.pow(x,y); raises x to the y power
Math.random(); a pseudo-random number between 0.0 and 1.0
Math.round(x); rounds x to nearst integer
Math.sin(x); the sine of x
Math.sqrt(x); the square root of x (NaN if<0)
[Note- use powers to find other roots such as a cube root: cuberoot=Math.pow(27,1/3); cuberoot==3]
Math.tan(x); the tangent of angle x

Easy Pad

Blue Line
Lessons by
Jerry aka WebWizard