# Introduction to JavaScript – Variables: Mathematical Assignment Operators

Introduction to JavaScript – Variables: Mathematical Assignment Operators

In this exercise, let’s consider how we can use variables and math operators to calculate new values and assign them to a variable. Check out the example below:

```
let x = 4;
x = x + 1;

```

In the example above, line 1) we created the variable x with the number 4 assigned to it. On the following line 2), x = x + 1; increases the value of x from 4 to 5.

Notice, on line two in the example above, to increment x by one we had to write the x variable on the left and right side of the assignment operator (=). Using a variable twice in one expression is redundant and confusing.

To address this, JavaScript has a collection of built-in mathematical assignment operators that make it easy to calculate a new value and assign it to the same variable without writing the variable twice. See examples of these operators below.

```
let x = 4;
x += 2; // x equals 6

let y = 4;
y -= 2; // y equals 2

let z = 4;
z *= 2; // z equals 8

let r = 4;
r++; // r equals 5

let t = 4;
t--; // t equals 3

```

In the example above, operators are used to calculate a new value and assign it to the same variable. Let’s consider the first three and last two operators separately:

1) The first three operators (+=, -=, and *=) perform the mathematical operation of the first operator (+, , or *) using the number on the right, then assign the new value to the variable.

2) The last two operators are the increment (++) and decrement () operators. These operators are responsible for increasing and decreasing a number variable by one, respectively.

# Exercise: Using what we have learned above complete the following:

```let molecule = 16;
let particle = 18;
let assay = 3;

// Add and assign to molecule below

// Multiply and assign to particle below

// Increment assay by 1

```

## Instructions:

1.
Use a mathematical assignment operator to add 16 to the value saved to molecule.

2.
Use a mathematical assignment operator to assign particle the value of itself multiplied by 6.02.

3.
Use the increment operator to increase the value saved to assay by 1.

4. Give up? See the answer code below.

## Introduction to JavaScript – Variables: Mathematical Assignment Operators

```
let molecule = 16;
let particle = 18;
let assay = 3;

// Add and assign to molecule below
molecule += 16;

// Multiply and assign to particle below
particle *= 6.02;

// Increment assay by 1
assay++;

```

# Related Videos:

My little pony learning game in VB.NET

Introduction to JavaScript – Variables: Undefined

Introduction to JavaScript – Variables: String Interpolation

Introduction to JavaScript – Variables: Review

Learn Partial functions Python programming

Tips and Tricks for WRITING JAVASCRIPT code

Python Basic Operators

Hello World Android app built with Android Studio

Who is this Android App Development course for?

FAQs

Online JavaScript Compiler. Code on the go.

JavaScript Glossary – Variables

How to make a go-back button with PHP code?

Introduction to JavaScript – Control Flow: True and False values

## About the Author: Bernard Aybout

In the land of bytes and bits, a father of three sits, With a heart for tech and coding kits, in IT he never quits. At Magna's door, he took his stance, in Canada's wide expanse, At Karmax Heavy Stamping - Cosma's dance, he gave his career a chance. With a passion deep for teaching code, to the young minds he showed, The path where digital seeds are sowed, in critical thinking mode. But alas, not all was bright and fair, at Magna's lair, oh despair, Harassment, intimidation, a chilling air, made the workplace hard to bear. Management's maze and morale's dip, made our hero's spirit flip, In a demoralizing grip, his well-being began to slip. So he bid adieu to Magna's scene, from the division not so serene, Yet in tech, his interest keen, continues to inspire and convene.