Many programming languages have multiple data types to store numeric values.
Specifically, programming languages generally divide quantitative variables into (at least) two categories:
Integers, often labelled as int
Floating-point numbers, often labelled as float
We can create a number variable in the same way that we created string variables previously - with either the var, let, or const keywords:
var year =2020;let age =20;const carYear =2009;
Here is how we could add var1 and var2 together:
var1 + var2;
Here's what this code would return:
Here's how we could subtract var2 from var1:
var1 - var2;
This code would return 5.
var1 * var2;
This statement would return 50.
var1 / var2;
This statement would return 2.
var1 ** var2;
The output of this code is 100000, which is 10 raised to the power of 5.
The remainder function is a lesser-known mathematical function defined as the polynomial “left over” after dividing one polynomial by another. As an example, the remainder of 18 divided by 4 is 2, because 16 divides evenly into 4 and 18 minus 16 is 2.
var1 % var2;
The output of this code is actually 0 since var1 actually divides evenly into var2.
Let's consider another example to make sure that you have a firm understanding of this topic:
This statement returns 1.
Throughout the rest of this tutorial, we will see how to access attributes and methods from within the Math object using the dot operator.
There are certain cases in which you will want to force a number to round up or round down when the Math.round method will normally cause the opposite behavior. We will learn how to handle these two situations in the next two sections of this tutorial.
Here are a few examples of the Math.floor method in action: