Broad Network


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.

Additive Operators
Additive Operators are addition and subtraction operators.

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

<!DOCTYPE HTML>
<html>
<head>
</head>
<body>

<script type="text/ECMAScript">

        id1 = 20;
        id2 = 30;
        
        id3 = id2 + id1;
        alert(id3);

</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;
        alert( id3);

    </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;
        alert(id3);

    </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;
        alert(id3);

    </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;
        alert(id3);

    </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;    

        alert(intA);
        alert(intB);

    </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

Related Links

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
Advanced Course
Advanced ECMAScript Regular Expressions
Promise in ECMAScript 2015
Generator in ECMAScript 2015
ECMAScript Module
More Related Links
Node Mailsend
EMySQL API
Node.js Web Development Course
Major in Website Design
Low Level Programming - Writing ECMAScript Module
ECMAScript Course

BACK NEXT

Comments

Become the Writer's Follower
Send the Writer a Message