Assembly in Progress...

Math in JavaScript

JavaScript has a variety of mathematical functions and operators. The basic set is listed below. For more advance functions, see MDN's list.

NOTE : The *= syntax can be used to add, subtract or multiply itself, and it works with all math operators: +=, -=, etc.

//////////////////
//    Addition
//////////////////
// Number + Number -> addition
1 + 2 // 3
// Boolean + Number -> addition
true + 1 // 2
// Boolean + Boolean -> addition
false + false // 0
// Number + String -> concatenation
5 + 'foo' // "5foo"
// String + Boolean -> concatenation
'foo' + false // "foofalse"
// String + String -> concatenation
'foo' + 'bar' // "foobar"


//////////////////
//    Subtraction
//////////////////
5 - 3 // 2
3 - 5 // -2
'foo' - 3 // NaN


//////////////////
// Multipication
//////////////////
2 * 2 // 4
-2 * 2 // -4
Infinity * 0 // NaN
Infinity * Infinity // Infinity
'foo' * 2 // NaN


//////////////////
//    Division
//////////////////
1 / 2     // returns 0.5 in JavaScript
1 / 2     // returns 0 in Java
// (neither number is explicitly a floating point number)

1.0 / 2.0 // returns 0.5 in both JavaScript and Java

2.0 / 0    // returns Infinity in JavaScript
2.0 / 0.0 // returns Infinity too
2.0 / -0.0 // returns -Infinity in JavaScript


//////////////////
//    Remainder
//////////////////
12 % 5 // 2
-1 % 2 // -1
1 % -2 // 1
NaN % 2 // NaN
1 % 2 // 1
2 % 3 // 2
-4 % 2 // -0
5.5 % 2 // 1.5


//////////////////
//    Exponents
//////////////////
2 ** 3 // 8
3 ** 2 // 9
3 ** 2.5 // 15.588457268119896
10 ** -1 // 0.1
NaN ** 2 // NaN

2 ** 3 ** 2 // 512
2 ** (3 ** 2) // 512
(2 ** 3) ** 2 // 64
//    To invert the sign of the result of an exponentiation expression:
-(2 ** 2) // -4
//    To force the base of an exponentiation expression to be a negative number:
(-2) ** 2 // 4


//////////////////
//    Increment
//////////////////
// Postfix
var x = 3;
y = x++; // y = 3, x = 4

// Prefix
var a = 2;
b = ++a; // a = 3, b = 3


//////////////////
//    Decrement
//////////////////
// Postfix
var x = 3;
y = x--; // y = 3, x = 2

// Prefix
var a = 2;
b = --a; // a = 1, b = 1


//////////////////
//Unary Negation(-)
//////////////////
var x = 3;
y = -x; // y = -3, x = 3

// Unary negation operator can convert non-numbers into a number
var x = "4";
y = -x; // y = -4


//////////////////
//Unary Plus(+)
//////////////////
+3     // 3
+'3' // 3
+true // 1
+false // 0
+null // 0
+function(val){ return val } // NaN


//////////////////
//    Built-In
//    Functions
//////////////////
// PI
Math.PI // 3.141592653589793

// The square root of a number
Math.sqrt(9) // 3

// Exponents
Math.pow(2,4) // 16

// Floor - This always rounds downto a whole number or integer
Math.floor(3.8) // Use whole number w/no rounding: 3

// Ceiling - This always rounds up to a whole number or integer
Math.ceil(3.8) - Use next higher whole number: 4

// Rounding
Math.round(3.2) // Use next lower whole number: 3
Math.round(3.8) // Use next higher whole number: 4

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

q
↑ Back to Top