(click anywhere to close)
OPEN MENU

[JavaScript] Numbers

category: Website | course: JavaScript | difficulty:

Numbers are of no use if we can’t perform calculations with them, and that’s what this chapter will talk about! We’ll also look at how to convert other data types to numbers.

Arithmetic

The four basic arithmetic operators you expect to see are here, and even more.

Operator

Description

+

Addition

-

Subtraction

*

Multiplication

/

Division

++

Increment (add 1)

--

Decrement (subtract 1)

%

Modulus*

console.log(5 + 5); //Prints 10
console.log(10 / 2); //Prints 5

Just as you normally would, you can put parentheses ( ( ) ) around equations to group them.

console.log((5 + 5) / 2); //Prints 5
console.log(5 + 5 / 2); //Prints 7.5

More complicated math is handled by the Math object, which is the topic of next chapter.

Sometimes, performing operations with floating point numbers doesn’t behave as you expect, and the result differs a tiny amount from the actual value. These differences are so small you often won’t even notice them, but if they are becoming a problem, it helps to first multiply them to get a whole number, and then divide again to get your answer.

console.log(0.1+0.2); //Prints 0.30000000000000004, instead of 0.3
console.log((0.1*10+0.2*10)/10); //Prints 0.3, as expected

Assignment

In the real world, people often write something like 4 + 5 = 9. In programming, you can’t do this. Everything needs to be statements that assign a value to a variable, where the variable needs to be on the left, and the value on the right. Therefore, the only way to change a value, is by assigning its old value again, plus some computations.

var x = 5;
x = x + 2; // x is now 7
var y = x*2; // y is now 14

Of course, this seems tedious, and a shorter and clearer way of writing these types of mathematical assignments is available.

Operator

Shorthand

Full

+=

x += y

x = x + y

-=

x -= y

x = x - y

*=

x *= y

x = x * y

/=

x /= y

x = x / y

%=

x %= y

x = x % y

var x = 5;
x += 2; // x is now 7
x *= 2; // x is now 14

I think this is a very important concept to understand completely, as you’ll need it very often, and it works the exact same way in all programming languages. You just need to remind yourself that you can’t have loose data – everything needs to be saved into a variable.

Knowing this, we can see that the ++ and -- operators are just shortcuts for += 1 and -= 1.

Conversion

To convert something to a number, use the parseInt(value) and parseFloat(value) methods.

var x = "100";
console.log(parseInt(x)); //Prints x as a number, without quotes

It will look for a number in the first characters of the value, but if it can’t find it, it will return the value NaN. It means “Not a Number”, which is a value you can’t really do anything with, besides check whether something is a number. This is also done with the isNaN(value) method.

var x = 2 / "JAMES BOND";
isNaN(x); //Should return true.

You’ll typically run into NaN errors when you (accidentally) perform mathematical operations on values that are not, or only partly, numbers. Like dividing 2 by a String.

Restrictions

Of course computers can’t support infinitely large numbers, which means there are some restrictions and oddities to be aware of. In JS, all numbers are 64-bit floating point numbers, which simply means that any number, regardless of length or type, is stored the same way, which causes some errors down the road.

It can handle integers up to 15 digits accurately. If longer, it will be off by a small margin.

It can handle a maximum amount of 17 decimals, but floating point numbers can also be off by a tiny margin, as you just saw.

You can find the upper and lower bound by accessing some properties on the global Number Object:

Property

Description

MAX_VALUE

Largest number possible in JS

MIN_VALUE

Smallest number possible in JS

POSITIVE_INFINITY

Represents numbers higher than JS can handle

NEGATIVE_INFINITY

Represents numbers lower than JS can handle

console.log(Number.MAX_VALUE); //Prints 1.7976931348623157e+308

When JS encounters numbers higher than its maximum, it will set the value to Infinity.

*Modulus

The modulus of x and y simply means the number that remains when you subtract all multiples of y from x.

4%2 equals 0 => 4 – 2 – 2 = 0

10%4 equals 2 => 10 – 4 – 4 = 2

14%5 equals 4 => 14 – 5 – 5 = 4

CONTINUE WITH THIS COURSE
Do you like my tutorials?
To keep this site running, donate some motivational food!
Crisps
(€2.00)
Chocolate Milk
(€3.50)
Pizza
(€5.00)