# Assignment Operators in ECMAScript 2015

## ECMAScript Operators � Part 1

### ECMAScript 6

Foreword: In this part of the series, I talk about the assignment operators in ECMAScript.

By: Chrysanthus Date Published: 13 May 2016

### Introduction

This is part 1 of my series, ECMAScript Operators. Addition and subtraction symbols are examples of operators in ECMAScript. ECMAScript has many other operators that do not have similarities with mathematics. In this part of the series, I talk about the assignment operators in ECMAScript.

Pre-Knowkedge
You need to have completed the following series before reaching here.

- ECMAScript Basics

A computer language builds up. There are certain things you have to learn first and then use them to learn higher things. That is why you should have read the series indicated above, before reaching here. The link to the series is given below (bottom of page).

In this part of the series, we look at assignment operators in ECMAScript. An example of such an operator is = .

Operand
An Operand is an identifier (variable) or a literal (value) associated with an operator. Consider,

myVar = 30;

myVar is a left operand and 30 is a right operand to =. = is an example of an operator. It is called, the assignment operator, not the equal operator (details below).

Consider:

myVar && hisVar && herVar

There are three operands in this expression. So, you can talk of the first (myVar), second (hisVar) and third (herVar) operands. &&, that is 2 ampersands form an operator; we shall talk about that later.
Binary and Unary Operators
A binary operator needs two operands to work with: one on its left and one on its right. A unary operator needs only one operand to work with, placed on its left or on its right (see later).

Assignment Operators
Assignment operators are:

=  *=  /=  %=  +=  -= .=

We look at each of these operators in this part of the series.

Simple Assignment, =
The basic assignment operator is, =. The following statement illustrates an example of its use:

myInt = 35;

We say the integer, 35, is assigned to the identifier, myInt. The left operand to = is myInt. The right operand is 35.

For basic or simple assignment, the right operand may be another identifier or even a function call that returns a value.

Compound Assignment Operators
= is actually the simple assignment operator. A compound assignment operator is made up of two operators: some other operator followed by the assignment operator.

E1 op= E2 Expressions
In the following generalized statement, E1 is an identifier, op is an operator and E2 is another identifier; = is the simple assignment operator. This statement is used to explain the coding of the compound operator.

E1 op= E2

This generalized statement is equivalent to,

E1 = E1 op E2

The rest of the assignment operators in the list above follow this generalized rule. The purposes of the assignment operators are explained below. These compound assignment operators are each made up of the simple assignment operator and an operator you might already know.
The *= Operator
Consider the following statement:

int1 = int1 * int2;

Here, int1 and int2 are identifiers. Assume that a value has already been assigned to int1 previously, before this statement. Note that int1 is found in both the left operand and the right operand (int1 * int2), of the simple assignment operator. We also have the multiplication operator, *. In the above statement the result of multiplying int1 by int2 is assigned to int1. The statement can be re-written as:

int1 *= int2;

Also, int2 can be replaced by a number as in,

int1 *=3;

The /= Operator
Consider the following statement:

int1 = int1 / int2;

Here, int1 and int2 are identifiers. Assume that a value has already been assigned to int1 previously, before this statement. Note that int1 is found in both the left operand and the right operand of =. We also have the division operator, /. In the above statement the result of dividing int1 by int2 is assigned to int1. The statement can be re-written as:

int1 /= int2;

Also, int2 can be replaced by a number as in,

int1 /=4;

The following program illustrates this:

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

<script type="text/ECMAScript">

int1 = 20;
int2 = 4;

int1 /= int2;
alert(int1);

</script>

</body>
</html>

The extra HTML code is to produce a web page.
The %= Operator
Consider the following statement:

int1 = int1 % int2;

Here, int1 and int2 are identifiers. Note that int1 is found in both the left operand and right operand. We also have the modulus operator, %. In the above statement the remainder of dividing int1 by int2 is assigned to int1. The statement can be re-written as:

int1 %= int2;

The += Operator
Consider the following statement:

int1 = int1 + int2;

Here, int1 and int2 are identifiers. Note that int1 is found in both the left operand and the right operand of the simple assignment operator. We also have the addition operator, +. In the above statement the result of adding int1 to int2 is assigned to int1. The statement can be re-written as:

int1 += int2;

The -= Operator
Consider the following statement:

int1 = int1 - int2;

Here, int1 and int2 are identifiers. Note that int1 is found in both the left operand and the right operand of the simple assignment operator. We also have the subtraction operator, -. In the above statement the result of subtracting int2 from int1, is assigned to int1. The statement can be re-written as:

int1 -= int2;

String Operators
There are two operators that can be used with strings, which are

+ and .=

To concatenate two strings as identifiers or as literals, use the + operator. Try this example:

<script type="text/ECMAScript">

str = "one "+"two";
alert(str);

</script>

In the script code, the plus (+) operator concatenates two strings.
Hey, you can use the E1 op= E2 compound operation with the string + operator.
Example:

<script type="text/ECMAScript">

str = "one ";
str +="two";
alert(str);

</script>

Try the code.  The + operator here, does concatenation and not addition as in math.

With that we come to the end of Assignment Operators. See you 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

NEXT