2.1: Arithmetic Operators | Mathematical Expressions

Learning Objectives

  • Be familiarized with the list of arithmetic operators available

  • Try writing your own mathematical expressions!

  • Use the addition operator on strings

Introduction

Arithmetic operators takes numerical values as their operands (the values on both sides of the operator) and returns a single numerical value.

6 + 13 // '+' is the operator; both values, '6' and '13', are the operands

In the case above, 13, being on the right side, is added to 6, on the left.

The standard arithmetic operators are addition (+), subtraction (-), multiplication (*), and division (/). These operators work as they do in most other programming languages when used with floating point numbers (decimals).

Here is a list of arithmetic operators:

OperatorDescriptionExample

Addition ( + )

a + b Result: adds b into a.

3 + 9 Result: 12

Subtraction ( - )

a - b Result: subtracts b from a

6 - 8 Result: -2

Multiplication ( * )

a * b Result: a is multiplied by b

4 * 2 Result: 8

Division ( / )

a / b Result: a is divided by b

9 / 3 Result: 3

Remainder or Modulo ( % )

a % b Result: remainder of a divided by b

10 % 6 Result: 4

Increment ( ++ )

a++ Result: adds 1 to a

Decrement ( -- )

a-- Result: subtracts 1 from a

Exponential ( ** )

a ** b Result: a is multiplied by itself by b number of times.

String Concatenation (addition operator)

You are able to use the arithmetic addition operator to combine two strings together, this can also be known as concatenation of two strings. The same addition operator + that is used on numbers are applied to strings in this case.

Though it should be noted that if you add two different data types together this can cause type coercion. Which is a fancy way of saying, JavaScript will alter the datatypes to accommodate the logic used. If you want to know more about type coercion please read about it here.

Using the logical addition operator we can assign a combined string to a variable.

// String Concatenation using logical operators

const string = 'Good morning' + ' ' + 'class';

console.log(string)
// 'Good morning class'

In the example above we are taking the operands strings/ values Good morning , ' ' and class to create a single string reading Good morning class . They are being combined through the + sign which is the operator.

Last updated