TOC

The community is working on translating this tutorial into Polish, but it seems that no one has started the translation process for this article yet. If you can help us, then please click "More info".

Operators:

Arithmetic operators

Programming is, when you dig through all the abstraction layers and get to the bottom, all about handling 1's and 0's - it's pure math! Therefore it can't come as a big surprise that you can of course do all kinds of math with JavaScript. In fact, many JavaScript operators can be found as operators on a simple calculator as well, and they'll (mostly) do the exact same thing.

Basic arithmetic operators

You'll find all the four basic arithmetic operators in JavaScript: Add, subtract, multiply and divide. Let's quickly have a look at each one of them.

The addition operator: +

Adding two numbers in JavaScript is very simple - just use the add (plus) operator. You can use numbers, like this:

let result = 2 + 3;
alert(result);

Or you can use variables, like this:

let a = 2, b = 3;
let c = a + b;
alert(c);

Just remember that the behavior of the add operator changes if you mix in other types, e.g. a string. In that case, the add operator will treat all values as string and concatenate them instead of doing math on them. We talked a bit about this in the chapter on data types, but let's just illustrate with an example:

let a = "2", b = 3;
let c = a + b;
alert(c);

The result here is not 5, but instead 23, because a combined text string of "2" and "3" are created as a result of the addition.

The subtraction operator: -

Subtracting two numbers is just as easy - just use the subtraction (minus) operator:

let answer = 5 - 2;
alert(answer);

Or you can use variables:

let a = 5, b = 2;
let c = a - b;
alert(c);

Now, we saw that the behavior changed when we added two variables of different types (e.g. a string and a number). The subtraction operator works a bit differently. It will instead try to convert participating members into numbers, to make the subtraction operation possible:

let a = "5", b = 2;
let c = a - b;
alert(c);

The result here is "3", just as in the example with all numbers, because JavaScript converts the text string of "5" into a number of 5 when it realizes that you are trying to subtract the two values.

The multiplication operator: *

Multiplying numbers is simple as well:

let answer = 5 * 2;
alert(answer);

And again, you can do it with actual numbers or variables containing numbers:

let a = 5, b = 2;
let c = a * b;
alert(c);

If you mix in other types than numbers, e.g. text strings, JavaScript will try to convert them for you, before doing the calculation:

let a = "5", b = 2;
let c = a * b;
alert(c);

The division operator: /

Doing division in JavaScript is quite easy as well, as you can see in this example:

let answer = 10 / 2;
alert(answer);

With numbers, as well as with variables:

let a = 10, b = 2;
let c = a / b;
alert(c);

As we saw with subtraction and multiplication, JavaScript will try to convert any other types than numbers (like text strings) into numbers, if you try to do division with them:

let a = "10", b = "2";
let c = a / b;
alert(c);

Something that usually requires extra attention when doing division is the number of 0 (zero). Some programming languages will actually throw an exception and halt execution if you try to do the (impossible) task of dividing something by zero. However, JavaScript is not that harsh, as you can see if you try to run this example:

let a = 10, b = 0;
let c = a / b;
alert(c);

Instead of throwing errors, it will return the special, global object called "Infinity".

Summary

Doing math in JavaScript is quite easy, as I've hopefully illustrated in the examples above - there's a well-known operator for each of the basic arithmetic operations you might want to perform. There are a few more math related operators, but we'll get to them as well in the following articles.


This article has been fully translated into the following languages: Is your preferred language not on the list? Click here to help us translate this article into your language!