# Arithmetic Operators in ECMAScript 2015

## ECMAScript Operators – Part 2

### ECMAScript 6

Foreword: In this part of the series we look at Arithmetic Operators in ECMAScript.

By: Chrysanthus Date Published: 13 May 2016

### Introduction

This is part 2 of my series, ECMAScript Operators. In this part of the series we look at Arithmetic Operators in ECMAScript. If you are new to programming, then you should have read the previous part of the series before reaching here.

Read and try the following code. The explanation is given below.

<!DOCTYPE HTML>
<html>
<body>

<script type="text/ECMAScript">

id1 = 20;
id2 = 30;

id3 = id2 + id1;

</script>

</body>
</html>

The extra HTML code is to produce a web page.
20 is kept in a region in memory identified by id1. 30 is kept in the region identified by id2. In the third statement, ECMAScript takes the content of id2 and adds it to the content of id1, then it puts the result as content for the region of the newly declared identifier (variable), id3. This addition is done without affecting or changing the contents of id2 and id1.

Subtraction Operator
Read and try the following code:

<script type="text/ECMAScript">

id1 = 20;
id2 = 30;

id3 = id2 - id1;

</script>

The explanation is similar to the previous case, but this time, subtraction is done.

Multiplicative Operators
Multiplicative operators are the *, / and % operators. See explanations below:

Multiplication Operator
Read and try the following code.

<script type="text/ECMAScript">

id1 = 20;
id2 = 30;

id3 = id2 * id1;

</script>

Note that the multiplication operator is * and not X. * multiplies two numbers.
Division Operator
Read and try the following code. The explanation is given below.

<script type="text/ECMAScript">

id1 = 3;
id2 = 15;

id3 = id2 / id1;

</script>

Note that the division operator is, / . / divides one number by another.

Modulus Operator
The modulus operator divides the first operand by the second operand and returns the remainder. Read and try the following code:

<script type="text/ECMAScript">

id1 = 17;
id2 = 12;

id3 = id1 % id2;

</script>

The Modulus operator is the percentage sign.

The – Operator
This is the negation operator. If the value of the operand is a positive number, it changes it to a negative number. If it is a negative number, it changes it to a positive number. This same symbol is used for subtraction, but here it is a negation operator. The following example illustrates this:

<script type="text/ECMAScript">

int1 = +5;
int2 = -6;

intA = -int1;
intB = -int2;

</script>

If you try the above code, the value of intA will be –5 and the value of intB will be +6 (same as 6). Remember, in mathematics, +5 and 5 mean the same thing, but –5 and 5 are two different things.

So there are 6 arithmetic operators that are:

+  - * / % negation

That is it for arithmetic operators. We take a break here and continue in the next part of the series.

Chrys

ECMAScript Basics
ECMAScript Operators
Expressions in ECMAScript
Statements in ECMAScript
Custom Objects in ECMAScript
Functions in ECMAScript
ECMAScript Date Object
The ECMAScript String Object
ECMAScript String Regular Expressions
ECMAScript Template Literal
The ECMAScript Array
ECMAScript Sets and Maps
ECMAScript Number
Scopes in ECMAScript
Mastering the ECMAScript (JavaScript) eval Function
Sending Email with ECMAScript
ECMAScript Insecurities and Prevention
Promise in ECMAScript 2015
Generator in ECMAScript 2015
ECMAScript Module