# 🧮 JavaScript Operators

May 10, 2020 ● Tag: javascript

Numeric data in JavaScript is represented by the `Number`

data type.

## Add Two Numbers

In JavaScript, like in mathematics, the `+`

symbol is used as an addition operator when in between two number.

Example:

1var randomNumber = 7 + 10; // 17

## Subtract One Number from Another

JavaScript uses the `-`

symbol for subtraction, in order to subtract one number from another:

1var randomNumber = 10 - 5; // 5

## Multiply Two Numbers

JavaScript uses the `*`

symbol for multiplication of two numbers:

1var randomNumber = 10 * 10; // 100

## Divide One Number by Another

JavaScript uses the `/`

symbol in order to divide one number by another:

1var randomNumber = 100 / 10; // 10

## Increment a Number

In JavaScript, we can use the `++`

operator to increment or add one to a variable:

1i++;

is the equivalent of

1i = i + 1;

## Decrement a Number

In JavaScript, we can use the `--`

operator to decrement or decrease a variable value by one:

1i--;

is the equivalent of

1i = i - 1;

## Create Decimal Numbers

Variables can store also decimal number, which are referred to also as `floating point numbers`

or simply `floats`

.

1var myDecimal = 9.9;

## Multiply Two Decimals

Just like with whole numbers, in JavaScript, we can perform calculations also with decimal numbers:

1var product = 2.0 * 2.5; // 5

## Divide One Decimal by Another

Now let's divide one decimal by another:

1var quotient = 6.6 / 2.0; // 3.3

## Finding a Remainder

In programming, it's common to check if a number is `even`

(divisible by 2) or `odd`

(not divisible by 2).

The remainder operator `%`

gives the remainder of the division of two numbers:

117 % 2 = 1; // 17 ==> Odd210 % 2 = 0; // 10 ==> Even

We can break this down by:

1Math.floor(17 / 2) = 8;28 * 2 = 16;317 - 16 = 1; // Remainder

## Compound Assignment With Augmented Addition

In programming, we use assignments to modify the content of a variable. Moreover, other than assigning a new value, we saw also how to perform basic mathematical operation.

In JavaScript, we can combine a mathematical operator and the assignment operator and obtain a new operator used for augmented addition `+=`

:

1var myFavoriteNumber = 1;2myFavoriteNumber += 16;3console.log(myFavoriteNumber); // 17

## Compound Assignment With Augmented Subtraction

Just like the `+=`

operator we saw before, `-=`

subtracts a number from a variable:

1var myFavoriteNumber = 4;2myFavoriteNumber -= 1;3console.log(myFavoriteNumber); // 3

## Compound Assignment With Augmented Multiplication

Using the `*=`

operator, we can multiply a variable by a number:

1var myFavoriteNumber = 5;2myFavoriteNumber *= 6;3console.log(myFavoriteNumber); // 30

## Compound Assignment With Augmented Division

Using the `/=`

operator, we can divide a variable by another number:

1var myFavoriteNumber = 10;2myFavoriteNumber /= 2;3console.log(myFavoriteNumber); // 5