Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
5 views

JavaScript+calculations

The document provides an overview of JavaScript arithmetic operators, including binary and unary operations, as well as assignment operators. It also details the Math object, including its properties and methods, which support various mathematical calculations and constants. Examples of using these operators and methods are included to illustrate their functionality.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

JavaScript+calculations

The document provides an overview of JavaScript arithmetic operators, including binary and unary operations, as well as assignment operators. It also details the Math object, including its properties and methods, which support various mathematical calculations and constants. Examples of using these operators and methods are included to illustrate their functionality.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

H:\Websites\Localhost\SourceCode\JavaScript calculations.

js Thursday, November 12, 2015 1:54 PM

/*
ternary
alert(c ? t : f); // When c is true, there is no reason to evaluate f.
*/

var a = 4;
//--a;
var c = a*3; //9
console.log(c % 2);
var b = (a==5?'yes':'no'); //ternary
var d = Math.floor(Math.random()*1000);
console.log(d);

/*
JavaScript supports the following binary arithmetic operators:

+ addition
- subtraction
* multiplication
/ division (returns a floating-point value)
% modulus (returns the remainder)
JavaScript supports the following unary arithmetic operators:

+ unary conversion of string to number


- unary negation (reverses the sign)
++ increment (can be prefix or postfix)
-- decrement (can be prefix or postfix)

Assignment
= assign
+= add and assign
-= subtract and assign
*= multiply and assign
/= divide and assign
%= modulus and assign

MATH
The Math object contains various math-related constants (for example, π) and functions (for
example, cosine). (Note that the Math object has no constructor, unlike Array or Date. All its
methods are "static", that is "class" methods.) All the trigonometric functions use angles
expressed in radians, not degrees or grads.

Properties of the Math object


Property Returned value
rounded to 5 digits Description
Math.E 2.7183 e: Natural logarithm base
Math.LN2 0.69315 Natural logarithm of 2
Math.LN10 2.3026 Natural logarithm of 10
Math.LOG2E 1.4427 Logarithm to the base 2 of e
Math.LOG10E 0.43429 Logarithm to the base 10 of e
Math.PI 3.14159 π: circumference/diameter of a circle
Math.SQRT1_2 0.70711 Square root of ½
Math.SQRT2 1.4142 Square root of 2

-1-
H:\Websites\Localhost\SourceCode\JavaScript calculations.js Thursday, November 12, 2015 1:54 PM

Methods of the Math object


Example Returned value
rounded to 5 digits Description
Math.abs(-2.3) 2.3 Absolute value: (x < 0) ? -x : x
Math.acos(Math.SQRT1_2) 0.78540 rad. = 45° Arccosine
Math.asin(Math.SQRT1_2) 0.78540 rad. = 45° Arcsine
Math.atan(1) 0.78540 rad. = 45° Half circle arctangent (-π/2 to +π/2)
Math.atan2(-3.7, -3.7) -2.3562 rad. = -135° Whole circle arctangent (-π to +π)
Math.ceil(1.1) 2 Ceiling: round up to smallest integer ≥ argument
Math.cos(Math.PI/4) 0.70711 Cosine
Math.exp(1) 2.7183 Exponential function: e raised to this power
Math.floor(1.9) 1 Floor: round down to largest integer ≤ argument
Math.log(Math.E) 1 Natural logarithm, base e
Math.max(1, -2) 1 Maximum: (x > y) ? x : y
Math.min(1, -2) -2 Minimum: (x < y) ? x : y
Math.pow(-3, 2) 9 Exponentiation (raised to the power of): Math.pow(x, y) gives xy
Math.random() 0.17068 Pseudorandom number between 0 (inclusive) and 1 (exclusive)
Math.round(1.5) 2 Round to the nearest integer; half fractions are rounded up (e.g. 1.5
rounds to 2)
Math.sin(Math.PI/4) 0.70711 Sine
Math.sqrt(49) 7 Square root
Math.tan(Math.PI/4) 1 Tangent
*/

-2-

You might also like