Help with JavaScript Snytax

Prof. Richard B. Goldstein

Mathematical constants
Javascript Value
Math.E
Math.LN10
Math.LN2
Math.LOG10E
Math.LOG2E
Math.PI
Math.SQRT1_2
Math.SQRT2
e=2.718...
ln(10)=2.3025...
ln(2)=0.693...
log10e=0.434...
log2e=1.44...
pi=3.14159...
1/sqrt(2)=0.707...
sqrt(2)=1.414...

JavaScript notation is
case sensitive

Mathematical functions
Javascript function
Math.abs(x)
Math.acos(x)
Math.asin(x)
Math.atan(x)
Math.atan2(y,x)
Math.ceil(x)
Math.cos(x)
Math.exp(x)
Math.floor(x)
Math.log(x)
Math.max(x,y)
Math.min(x,y)
Math.pow(x,y)
Math.random()
Math.round(x)
Math.sin(x)
Math.sqrt(x)
Math.tan(x)
absolute value of x
arccosine of x in radians
arcsin of x in radians
arctan of x in radians
arctan of y/x in radians (polar coordinates)
smallest integer >= x
cosine of x
ex
largest integer <= x
ln(x)
greater of x and y
lesser of x and y
xy
psuedo-random number on (0,1)
rounds to nearest integer
sine of x
square root of x
tangent of x

(x2 + 2x - 1)5/3 is Math.pow((x*x+2*x-1),5/3)
equation example is Math.sqrt(Math.sin(x)/(Math.pow(2,x)+3*Math.log(x)))

JavaScript
expression
x=7
y=1
x=7
y=-1
x=-7
y=1
x=-7
y=-1
comment
Simple Addition
x+y 71 7-1 -71 -7-1 fails
(x+y) 71 7-1 -71 -7-1 fails
eval(x)+eval(y) 8 6 -6 -8 works
x+2*y-y 71 NaN -73 NaN fails
eval(x+y) 71 6 -71 -8 mixed results
1*x+1*y 8 6 -6 -8 works
x-(-y) 8 6 -6 -8 works
s=0;
s += x;
s += y;
071 07-1 0-71 0-7-1 fails
Other Binary Operations
x-y 6 8 -8 -6 works
x*y 7 -7 -7 7 works
x/y 7 -7 -7 7 works

    The formulas and programs will work well for most functions. Note that JavaScript will require the 1 coefficient for simple additions. This is because it prefers working with strings. That is, y=a+b where a is 2.4 and b is 3.3 will not yield 5.7. Rather, it will get 2.43.3 a value it doesn't understand. Even worse, a = 2.5 and b = 3 will give 2.53 an incorrect number which it does understand. This problem occurs with the use of forms which assume values are in the "text" form. If internally in Javascript one sets a = 2.4 and b = 3.3, then the result of c=a+b; yields the correct 5.7.

Return to Statistics