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.

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

``````let result = 2 + 3;

Or you can use variables, like this:

``````let a = 2, b = 3;
let c = a + b;

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;

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;

Or you can use variables:

``````let a = 5, b = 2;
let c = a - b;

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;

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;

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

``````let a = 5, b = 2;
let c = a * b;

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;

### The division operator: /

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

``````let answer = 10 / 2;

With numbers, as well as with variables:

``````let a = 10, b = 2;
let c = a / b;

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;
``````let a = 10, b = 0;