3 min read

[5] Numbers and Math in JavaScript

[5] Numbers and Math in JavaScript
Photo by Mika Baumeister / Unsplash

They are just numbers!

One of the basic data types in JavaScript is Number. The reason you would want to work with the data type of numbers obviously to perform mathematical operations.

All data types have a different purpose and a different way to create variables of said types. In JavaScript, the syntax is the simplest of all other types. The value you assign to your variable is just what ever characters it takes to create it. No special characters need to wrapp the value, unlike strings with their quotation marks " " or ' '.

let myValue = 40

In many other programming languages there are various different number types that have differnt capabilities, such as: integers, floating point decimals, unsigned or absolute numbers, signed or possibly negative numbers and even further distinctions. In JavaScript, all number are effectively of the same data type number.

Quick note: there is something in JS called BigInt. We will ignore that data type for now as it is not needed to start working with numbers.

Floats

let myFloat = 3.14

Float is short term for a floating point decimal and simply put are numbers that can have a decimal value; this is how we work with fractions in programming languages. You do not need to specify a variable as a float instead of a number because in JS all numbers can have a floating point decimal. For example, 45 is virtually the same as 45.000. Knowing this term will not effect your ability to work with numbers, but it will help your ability to communicate with others about numbers.

Math

As stated before, we use the number data type so that we can perform mathematical operations on them, such as addition, subtraction, multiplication, and so on. If we only cared about a human reading a number, we might as well use a string data value like "45.00". More on strings in another article.

There are seveal standard ways to perform mathimatical operations. First there is the use of inline operators. Secondly there are math libraries, either those built into JS or 3rd party libraries.

Operators

In JS there are several special characters that tell the language to do certain tasks for us. These special characters are called operators and most of these are used in math calculations.

A great detailed list with explanations can be found here: https://www.w3schools.com/js/js_operators.asp

But we will cover a few that we will commonly use.

Operator Description
+ Addition
- Subtraction
* Multiplication
/ Division
++ Increment by one
-- Decrement by one

Examples:

5 + 10 	// 15
7 - 2 	// 5
3 * 4	// 12
12 / 6	// 2
5++	// 6
9--	// 8

With variables:

let a = 6
a--
console.log(a) // 5

let b = 50 + a
b = b - 10
console.log(b) // 45
b = b/a
console.log(b) // 9
b++
console.log(b) // 10
b = b * 30
console.log(b) // 300

Libraries

There are several 3rd party libraries that you kind find, but lets look at the standard Math object that comes with JS. (More on Objects in a future article)

Here is a great resource of the Math object with a list of functions/methods to perform more comlex calculations: https://www.w3schools.com/js/js_math.asp

The Math object have several constant properties (values that never change) of common values that you can access such as Pi.

// to access Pi from Math
let myValue = Math.PI
console.log(myValue) // 3.14......

There are also several methods (functions) on Math that you can use to calculate values by passing in parameters such as Sin, Cosin, exponentials, and rounding.

let myExponentValue = Math.pow(3,4) // 3 to the power of 4
console.log(myExponentValue) // 81

let myNegativeNumber = -65
let myAbsoluteNumber = Math.abs(myNegativeNumber) // absolute value
console.log(myAbsoluteNumber) // 65

There are so many opertators and Math helper methods to discover. You may not need to use most of them while you are learning, but make sure to briefly look over the list provided in the link above. Just knowing what exists will be a valubale asset while you are working on you learning projects.

Being a mathematician is not a prerequisite, or required at all, to being a software developer. Most math you use will be the basic addition and subtraction, multiplication and division. Looking over these methods and brushing up on very basic algerbra will also go a long way in some circumstances.