Property | Description | Approximate Value |
---|---|---|
Math.E | Euler's constant, \(e\) | 2.718 |
Math.LN2 | Natural (base \(e\)) logarithm of 2 | 2.303 |
Math.LN10 | Natural (base \(e\)) logarithm of 10 | 1.443 |
Math.LOG10E | Base 10 logarithm of \(e\) | 0.434 |
Math.PI | Ratio of circle's circumference to its diameter | 3.14159 |
Math.SQRT1_2 | Square root of 1/2 | 0.707 |
Math.SQRT2 | Square root of 2 | 1.414 |
Method | Returns | Example | Example Returns | Notes |
---|---|---|---|---|
Math.abs(x) | Absolute value (distance from 0) of \(x\) | Math.abs(-6.5) | 6.5 | |
Math.acos(x) | Angle \(\theta\) (in radians) such that \(\cos(\theta)=x\) | Math.acos(0.6) | 0.9272952180016123 | \(x\) must be between -1 and 1 |
Math.asin(x) | Angle \(\theta\) (in radians) such that \(\sin(\theta)=x\) | Math.asin(0.6) | 0.6435011087932844 | \(x\) must be between -1 and 1 |
Math.atan(x) | Angle \(\theta\) (in radians) such that \(\tan(\theta)=x\) | Math.atan(10) | 1.4711276743037345 | |
Math.atan2(x,y) | Angle \(\theta\) (in radians) such that \(\tan(\theta)=x/y\) | Math.atan2(20,2) | 1.4711276743037345 | \(y\) cannot be 0 |
Math.ceil(x) | Smallest integer greater than \(x\) | Math.ceil(4.23) | 5 | |
Math.cos(x) | \(\cos(x)\) | Math.cos(Math.PI) | -1 | \(x\) is an angle in radians |
Math.exp(x) | \(e^x\) | Math.ceil(4.23) | 4 | |
Math.floor(x) | Greatest integer less than \(x\) | Math.ceil(4.23) | 4 | |
Math.log(x) | Natural logarithm of \(x\) | Math.log(1) | 0 | \(x\) must be a positive number |
Math.max(list) | Largest value in list | Math.max(4,6,1,9,2,-5,2) | 9 | |
Math.min(list) | Smallest value in list | Math.min(4,6,1,9,2,-5,2) | -5 | |
Math.pow(x,y) | \(x^y\) | Math.pow(4,2) | 16 | Math.pow(x,-y) = \(\frac{1}{x^y}\) Math.pow(x,a/b) = \(\sqrt[b]{x^a}\) |
Math.random() | Pseudo-random value between 0 and 1 | Math.random() | 0.37454011430963874 | |
Math.round(x) | Closest integer to \(x\) | Math.round(3.14159) | 3 | |
Math.sin(x) | \(\sin(x)\) | Math.sin(Math.PI/2) | 1 | \(x\) is an angle in radians |
Math.sqrt(x) | Square root of \(x\) | Math.sqrt(25) | 5 | \(x\) is nonnegative number |
Math.tan(x) | \(\tan(x)\) | Math.tan(6) | -0.29100619138474915 | \(x\) is an angle in radians |
Property | Description | Approximate Value |
---|---|---|
Number.EPSILON | Smallest interval between two numbers (used to test 'close enough' equality: x-y < Number.EPSILON returns true if x and y are 'within epsilon' of each other) | \(2^{-52}\) |
Number.MAX_VALUE | Largest possible representable number | 1.7976931348623157e+308 |
Number.MIN_VALUE | Smallest possible representable number | 5e-324 |
Number.NaN | "Not a number"; equivalent to NaN | |
Number.NEGATIVE_INFINITY | Negative infinity, returned on overflow | |
Number.POSITIVE_INFINITY | Positive infinity, returned on overflow |
Instance | Returns | Example | Example Returns |
---|---|---|---|
object.toExponential() | String representing number object in exponential notation | var x=523423.3452; x.toExponential() | '5.234233452e+5' |
object.toFixed() | String representing number object in fixed point notation | var x=523423.3452; x.toFixed() | '523423' |
object.toPrecision(x) | String representing number object with precision \(x\) | var x=523423.3452; x.toPrecision(3) | '5.23e+5' |
object.toString() | String representing number object in base 10 | var x=523423.3452; x.toString() | '523423.3452' |
object.toString(b) | String representing number object in base \(b\) | var x=523423; x.toString(2) | '1111111110010011111' |
function randomBetween( a,b ) { return Math.floor(Math.random()*(b-a+1)+a); }
Operator | Description | Example | Example Returns |
---|---|---|---|
+ | Addition, String concatenation | 3+6 "hello "+"world" | 9 "hello world" |
- | Subtraction | 12-8 | 4 |
/ | Division | 12/3 | 4 |
* | Multiplication | 2*5 | 10 |
% | Modulus (remainder) | 17%5 | 2 |
++ | Increase by one | var x = 5; x++ | x is now 6 |
+x | Unary plus, evaluates operand and converts to number, if possible | +"45" +true | 45 1 |
Operator | Description | Example | Example Returns |
---|---|---|---|
x == y | Returns true if x and y can be converted to the same type and value | 5 == 5 5 == '5' 1 == true | true false true |
x != y | Returns true if x and y are of different types or values | 5 != 5 5 != '5' 1 != true | false true false |
x === y | Returns true if x and y are the same type and value | 5 === 5 5 === '5' 1 === true | true false false |
x !== y | Returns true if x and y are not the same type and value | 5 !== 5 5 !== '5' 1 !== true | false true true |
x > y | Returns true if x is strictly greater than y after type conversion | 5>2 6>'3' 2>5 | true true false |
x >= y | Returns true if x is greater than or equal to y after type conversion | 5>=7 6>='3' 5>=5 | false true true |
x < y | Returns true if x is strictly less than y after type conversion | 5<2 6<'10' 2<5 | false true true |
x <= y | Returns true if x is less than or equal to y after type conversion | 5<=7 6<='3' 5<=5 | true false true |
Operator | Description | Example | Example Returns |
---|---|---|---|
x = value | Assign a value to a variable x | var x = 5, y = "hello" console.log(x,y) | 5 'hello' |
x += y | Addition assignment, shorthand for x = x+y | x = 7; x += 5 | x is now 12 |
x -= y | Subtraction assignment, shorthand for x = x-y | x = 7; x -= 5 | x is now 2 |
x *= y | Multiplication assignment, shorthand for x = x*y | x = 7; x *= 5 | x is now 35 |
x /= y | Division assignment, shorthand for x = x/y | x = 10; x /= 5 | x is now 2 |
x %= y | Remainder assignment, shorthand for x = x%y | x = 10; x %= 3 | x is now 1 |
Operator | Description | Example | Example Returns |
---|---|---|---|
x && y | Returns true if both x AND y are true | (3 < 4) && (4 == 4) (3 < 4) && (4 > 4) | true false |
x || y | Returns true if either x OR y are true | (3 < 4) || (4 == 4) (3 < 4) || (4 > 4) (2 > 6) || (2 > 4) | true true false |
!x | Returns true if x is false | !(3 < 4) !(3 > 4) | false true |
var x = prompt( "Pick a number" ) // Decide if provided value is larger than 10, and choose a message accordingly: var returnMessage = ( x > 10 ) ? " larger than 10" : " less than or equal to 10" alert( "The number " + x + " is " + returnMessage )Try it
var x = prompt( "Find the absolute value of any number: " ) var absX = ( x >= 0 ) ? x : -x alert( "The absolute value of " + x + " is " + absX )Try it