JavaScript has plug-in libraries (Math, Science.js, etc.) that simplify complex actions related to trigonometry, statistics, and other special areas of mathematics. Yet their use would be limited or impossible without the basic arithmetic operations included in JavaScript by default.

JavaScript supports basic operations with numbers: addition, subtraction, multiplication, and division. Also, numbers can be equated to a certain value, compared with each other, inversed, etc. Arithmetic operations in JavaScript work according to generally basic rules of mathematics, if you retain required syntax.

Syntax

To perform an arithmetic operation in JavaScript, you need at least two numbers (or numeric variables), called operands. You can specify them directly, or you can implement them by setting expression executing using parentheses.

The operations with operands must be marked with the corresponding signs (plus, minus, etc.), called operators. The result of execution in most cases requires the implementation of a separate output, but in some cases it can change the original data directly.

Basic mathematical operations

  • Addition (+)

Addition in JavaScript is implemented via the + sign and can be applied to both numeric values and variables which implement them. For example:

let a = 2+3;
console.log(a);

Result:

5

or

let a = 2;
let b = 3;
let c = a+b;
console.log(c);

Result:

5

or

let a = 2;
let b = 3;
console.log(a+b);

Result:

5

Also, using + you can add string values. This operation is called concatenation. For string values, the same rules apply so you can work with variables, values, or their combinations:

let a = 'Test ';
let b = 'text';
console.log(a+b);
console.log(a+'text');

Result:

Test text
Test text

Also, you can add numbers and strings, but note that result of such an operation always will be a string value:

let a = 12;
let b =  ' - is a dozen';
console.log(a+b);

Result:

12 - is a dozen
  • Subtraction (-)

The subtraction operation can be performed for numeric values and variables only. If the subtracted value is greater than the reduced, then the result of the operation will be a number with a negative value.

let a = 12;
console.log(a-2);
console.log(2-a);

Result:

10
-10
  • Division (/)

You can use positive and negative numbers for division. Division by zero will return Infinity instead of an error, which stands for a global class in JavaScript. JavaScript also supports division with remainder, in this case the result will be converted to a decimal fraction.

let a = 12;
console.log(a/2);
console.log(-a/2);
console.log(a/5);

Result

6
-6
2.4
  • Multiplication (*)

It allows you to multiply any numbers: integers, fractions, negatives, etc. Multiplication by zero, same as in mathematics, will return zero.

let a = 3;
let b = 4;
console.log(a*b);

Result

12

Special operations

  • Remainder division (%)

A kind of division, also called "modulo operation" or division with remainder. Unlike normal division, it only returns the remainder of the division. If the result is less than one, it will be returned as a decimal fraction, and if there is no remainder, zero will be returned.

let a = 10;
console.log(a%3);
console.log(a%0.12);
console.log(a%5);

Result:

1
0.04000000000000037
0
  • Exponentiation (**)

The combination of two multiplication signs allows you to perform exponentiation. In such expressions, the number to the left of the ** operator will be raised to the size which sets the number to the right of the operator.

let a = 10;
let b = 2;
console.log(a**b);

Result

1
  • Increment and decrement (++ and --)

These operators are most often used in loops because they have specific functionality. They change the given number by 1, i.e. increment adds 1, decrement  subtracts 1. It is important to note that during execution actions with these operators, the values of the original variables will be changed.

Also, the returned value depends on where the operators were specified relative to the number. If they are specified before the number, then we get the variable changed by one:

let a = 3;
let b = 10;
console.log(++a);
console.log(--b);

Result

4
9

But if the operators are specified after the number, it returns the original number, and then it will be changed accordingly to the operator.

let a = 5;
let b = 2;
console.log(a++);
console.log(a);
console.log(b--);
console.log(b);

Result

5
6
2
1
  • Unary negation (-)

In JavaScript, Unary negation is an implementation of a mathematical rule that a minus placed before a number makes it the opposite of the current value. Accordingly, unary negation returns a positive number negative (and vice versa) without changing the original number.

let a = -5;
let b = 3;
console.log(-a);
console.log(-b);

Result

5
-3
  • Comparisons (>, <, >=, <=)

In JavaScript there are 4 options for comparing numbers, which requires the following operators:

a > b (а greater than b)

a < b (a less than b)

a >= b (а greater than b, or a equals b)

a <= b (a less then b, or a equals b)

If the equation is correct (both parts correspond to the operator), then true will be returned, and if not, then false.

let a = 5;
let b = 7;
let c = 7;
console.log(a>b);
console.log(a<b);
console.log(b>=c);
console.log(b<=c);

Result

false
true
true
true
  • Arithmetic assignments (+=, -=, *=, /=)

This is a group of operators that allow you to define simple arithmetic operations in a simpler form. After their execution the result will be assigned directly to the original variables. To use any of these 4 constructs, you must add an equal sign (=) in the equation after the arithmetic sign.

let a = 6;
let b = 6;
let c = 6;
let d = 6;
console.log(a += 2);
console.log(b -= 2);
console.log(c *= 2);
console.log(d /= 2);

Result

8
4
12
3

Since assignment changes the original number, we used a separate variable for each action. If there was only one variable, then the script would perform all the actions in the order of their mentioning, applying them in the code to the results of the previous actions.

Test yourself

  • Task 1. The record for the biggest weight among land animals belongs to the African elephant - 12.24 tonnes, while the estimated weight of an adult whale shark is approximately 18.6 tonnes. Find how many times the weight of a whale shark is greater than the weight of an African elephant.

Solution

First, let's create the elephant and shark variables to which we assign the appropriate weight values. Then, in the difference variable, we divide the shark value by the elephant value to get the desired ratio and get the result by printing the value of this variable to the console.

let elephant = 12.24;
let shark = 18.6;
let difference = shark/elephant;
console.log(difference);

Result

1.5245901639344264
  • Task 2. In the given row of numbers, increase the even numbers and decrease the odd ones using only Increment and Decrement operators. Print each number on a separate line to the console. Numbers: 12, 3, 19, 2, 7, 11.

Solution

Let's create a variable for each number, and then configure the output for each of them to the console. In the lines of the console.log()method calling, add operators denoting the operations Increment and Decrement.

let a = 12;
let b = 3;
let c = 19;
let d = 2;
let e = 7;
let f = 11;
console.log(++a);
console.log(--b);
console.log(--c);
console.log(++d);
console.log(--e);
console.log(--f);

Result

13
2
18
3
6
10
  • Task 3. For the numbers 5 and 2, perform all possible basic arithmetic operations. Print the results to the console.

Solution

We have not specified the order of the operands, so each of the four basic steps must be performed for both cases. But since in addition and multiplication, the change of places of numbers does not affect the result, we can exclude repeating operations.

let a = 5;
let b = 2;
console.log(a+b);
console.log(a-b);
console.log(b-a);
console.log(a*b);
console.log(a/b);
console.log(b/a);

Result

7
3
-3
10
2.5
0.4
  • Task 4. Get the result of raising the number 7 to the power of 3, and check the correctness of the calculations by implementing the same action using ordinary multiplication.

Solution

Exponentiation is performed using the ** operator, and in ordinary multiplication this can be repeated by multiplying 3 numbers 7 by each other.

let a = 7;
console.log (7**3);
console.log(7*7*7);

Result

343
343
  • Task 5. Perform exponentiation, multiplication, and division by the numbers 0.1 and 0.99 for 365.

Solution

To make it easier to write expressions, we will create three variables and assign values to them. Then, using operators, we implement the necessary operations directly in the parentheses of the console output method.

let a = 365;
let b = 0.1;
let c = 0.99;
console.log(a*b);
console.log(a*c);
console.log(a**b);
console.log(a**c);
console.log(a/b);
console.log(a/c);

Result

36.5
361.35
1.8039698981980867
344.0883239364977
3650
368.6868686868687
  • Task 6. Correct the equations by replacing = with operands after which all equations executions will return true.

console.log(47=9);
console.log(0.54601=0.5460001);
console.log(3856=8944);
console.log(19=9*2);
console.log(88=7**3);

Solution

console.log(47>9);
console.log(0.54601>0.5460001);
console.log(3856<8944);
console.log(19>9*2);
console.log(88<7**3);

Result

true
true
true
true
true