Javascript provides the tools needed to write code that implements the concepts, formulas, and functions found in algebra, trigonometry, and even calculus. Beginning with basic concepts from elementary school, we’ll walk through the way Javascript enables us to write computational programs.

Javascript Arithmetic Operators

Javascript provides the basic operators most familiar to us:

OperationOperator
Addition+
Subtraction
Multiplication*
Division/
Remainder (Modulus)%

Javascript operators in use appear as one might expect from math textbooks and any prior exposure to the Javascript let keyword. 

let a = 1 + 2 + 3 + 4 ;
let b = a - 2 ;
let c = a * b * 999 ;
let d = c / 3.14159 ;
let e = c % 2 ;

Javascript Arithmetic Order of Operations

Javascript implements an order of mathematical operations known as PEMDAS (Parentheses, Exponents, Multiplication and Division, Addition and Subtraction), evaluating the expression from left to right. Consider the following expression, where 5**2 means the exponentiation 52:

let w = 7 + (6 × 5**2 + 12) / 6 * 3 - 2 ;

We use parentheses to show first-order operations, so the above becomes:

let w = 7 + ((((6 × (52)) + 12) / 6) * 3) - 2 ;

(This is the same thing as scanning left to right and performing the sub-operatons in PEMDAS order.) Simplifying, showing our work, this becomes:

let w = 7 + ((((6 × 25) + 12) / 6) * 3) - 2 ;
let w = 7 + (((150 + 12) / 6) * 3) - 2 ;
let w = 7 + ((162 / 6) * 3) - 2 ;
let w = 7 + (27 * 3) - 2 ;
let w = 7 + 81 - 2 ;
let w = 86 ;

Javascript Arithmetic Data Types

Javascript handles the data typing automatically, using a variable container appropriate to the mathematical operation. For example:

let a = 10 / 2 ; // divides evenly
let b = 10 / 3 ; // doesn’t divide evenly, has a fractional part
console.log( "a=", a, ", b=", b ) ;

The output of a=5, b=3.3333333333333335 shows that one is considered an integer variable whereas the other a floating point variable.

Minimums and Maximums in Javascript

The functions Math.min() and Math.max() return the mathematical minimum and maximum, respectively. The following snippets show these functions working on integers, floating point numbers, and arrays.

// positive numbers
console.log(Math.min(1, 10, 237, 333.33, 448)) ; // ==> 1
console.log(Math.max(1, 10, 237, 333.33, 448)) ; // ==> 448

// negative numbers
console.log(Math.min(1, -10, 237, -333.33, -448)); // ==> -448

// arrays
const array_of_joy = [ 1, -10, 237, -333.33, -448 ] ;
console.log(Math.max(...array_of_joy)) ; // ==> 237

Modulus

The modulo operation returns the “remainder” of a division. The operator is the percent sign – %. The expression 5 % 2 returns a 1 because 5 is composed of two twos with 1 left over. We can use this function to code a pair of functions which test for an expression being odd or even in a way that mimics natural language (the way we think and speak):

function odd ( n ) { return n % 2 ; }
function even ( n ) { return ! odd(n) ; }
console.log( even(6 % 2) ) ;
console.log( even(5 % 2) ) ;

The above code returns the output:

true
false

Modulus is also the way we intuitively think about clock time. For example, if it’s 8pm now, what time will it be in 5 hours? If we’re using the 24-hour military time (used in much of the rest of the world even in civilian populations for everything from office meetings to television schedules) we’re good with 13 o’clock, but for the vast majority in the USA we think: 

console.log( (8 + 5) % 12 ) ; // ==> 1

Note, we mentally change the day/night marker from PM (post meridiem, after midday) to AM (ante meridiem, before midday) without thinking about it, and we’ll need to keep track when writing code which uses a 12-hour clock, but it all starts with the modulus operator.

Exponents in Javascript

We’ve already encountered exponentiation above; for completeness we’ll explicitly mention that x raised to the power y — xy —  is written in Javascript as x**y.

Square Roots in Javascript

Square roots in Javascript are done through the Math.sqrt() function:

console.log( Math.sqrt(9) ) ; // ==> 3

Rounding in Javascript

Rounding numbers to the nearest whole number is likewise done with a dedicated function, Math.round():

console.log( Math.round(2.49), Math.round(2.50) ) ; // ==> 2 3

Conclusion

In this blog entry we’ve covered

• the basic mathematical operations (addition, subtraction, multiplication, division)
• several of the other common operations (modulus, exponentiation, square roots, rounding)

Javascript is quite able to implement your computational needs.

Start Learning

To learn more about Javascript, check out other Javascript blog posts and enroll in our Javascript Nanodegree programs, including Intermediate Javascript.

Source Code

Complete source code, when needed to expand upon the snippets above, will be included below, ready for use in an online Javascript tester like PlayCode.