Operators

Overview

Operators

the 5 Arithmetic Operators

  1. = assignment
  2. + addition
  3. - subtraction
  4. * multiplication
  5. / division
  6. % modulo

Assignment Operator =

Addition Operator +

let augend = 5;
let addend = 10;
let sum = augend + addend; // `sum` is 15

Subtraction Operator -

let minuend = 15;
let subtrahend = 10;
let difference = minuend - subtrahend // `difference` is 5

Multiplication Operator *

let multiplicand = 5;
let multiplier = 10;
let product = multiplicand * multiplier // `product` is 50

Division Operator /

let dividend = 10
let divisor = 5
let quotient = dividend + divisor // `quotient` is 2

Modulo Operator %

let dividend = 10
let divisor = 5
let remainder = dividend + divisor // `quotient` is 2

the 2 string operators

the concatenation operator, +

let body = "Hello ";
let appendage = "World";
let result = body + appendage;

the multiplication operator, *

let multiplicand = "abc";
let multiplier = 3;
let product = multiplicand * multiplier # `product` is "abcabcabc";

the 6 Logical Operators

  1. not conditional negation
  2. and conditional and
  3. or conditional or

the unary not operator

let rightOperand = true;
let outcome = not rightOperand # outcome is `false`;
let rightOperand = false;
let outcome = not rightOperand # outcome is `true`;

the binary and operator

let doorIsUnlocked = door.isUnlocked();
let doorIsOpen = door.isOpen();
let canWalkThroughDoor = doorIsUnlocked and doorIsOpen;

the binary && operator

Example
let canWalkThroughDoor = door.isUnlocked() and door.isOpen()
let canWalkThroughDoor = false
if(door.isUnlocked()){
    if(door.isOpen()) {
        canWalkThroughDoor = true
    }
}

the binary or operator

let doorIsUnlocked = door.isUnlocked();
let doorIsOpen = door.isOpen();
let canWalkThroughDoor = doorIsUnlocked or doorIsOpen;

the binary or operator

the binary || operator

Example

let canWalkThroughDoor = door.isUnlocked() || door.isOpen();
let canWalkThroughDoor = false;

if(door.isUnlocked()){
    canWalkThroughDoor = true;
}

if(door.isOpen()){
    canWalkThroughDoor = true; 
}

Relational Operators

the 6 Relational Operators

  1. < less than
  2. > greater than
  3. <= less than or equal to
  4. >= greater than or equal to
  5. == equal to
  6. != not equal to

Relational Operators

< Less than

operand1 = 0
operand2 = 1
outcome = operand1 < operand2 # outcome is `true`
operand1 = 1
operand2 = 0
outcome = operand1 < operand2 # outcome is `false`

> Greater than

operand1 = 1
operand2 = 0
outcome = operand1 > operand2 # outcome is `true`
operand1 = 0
operand2 = 1
outcome = operand1 > operand2 # outcome is `false`

<= Less than or equal to

operand1 = 0
operand2 = 1
outcome = operand1 <= operand2 # outcome is `true`
operand1 = 1
operand2 = 0
outcome = operand1 <= operand2 # outcome is `false`
operand1 = 0
operand2 = 0
outcome = operand1 <= operand2 # outcome is `true`

>= Greater than or equal to

operand1 = 0
operand2 = 1
outcome = operand1 >= operand2 # outcome is `false`
operand1 = 1
operand2 = 0
outcome = operand1 >= operand2 # outcome is `true`
operand1 = 0
operand2 = 0
outcome = operand1 >= operand2 # outcome is `true`

== Equal to

operand1 = 0
operand2 = 1
outcome = operand1 == operand2 # outcome is `false`
operand1 = 1
operand2 = 0
outcome = operand1 == operand2 # outcome is `false`
operand1 = 0
operand2 = 0
outcome = operand1 == operand2 # outcome is `true`

Increment / Decrement Operators

+= Increment Operator

startingValue = 10
startingValue += 5 # startingValue is `15`
startingValue = 10
startingValue = startingValue + 5 # startingValue is `15`

-=, Decrement Operator

startingValue = 10
startingValue -= 5 # startingValue is `5`
startingValue = 10
startingValue = startingValue - 5 # startingValue is `5`

Parentheses and Operator Hierarchy

4 + 5 * 8 == 44
(4 + 5) * 8 == 72

Since += associates right to left, the expression

a += b += c

means

a += (b += c)