const - JS | lectureJavaScript Fundamentals 1

Basic Operators

JavaScript Fundamentals 1

An operator allows us to transform values or combine multiple values. There are many categories of operators .i.e. mathematical operators, logical operators, assignment operators etc.

Mathematical or Arithmetic Operators

We recently used the plus ( + ) operator, but of course there are more such as; multiplication ( * ), division ( / ), minus ( - ) etc.

Minus

script.js
const numberOfApples = 100;
const amountBought = 12
console.log(numberOfApples - amountBought) // Result ==> 88

Multiplication

script.js
console.log(amountBought * 2) // Result ==> 24

Division

script.js
console.log(amountBought / 10) // Result ==> 1.2

Exponentiation

script.js
// 2 ** 3 means 2 to the power of 3 .i.e. 2 x 2 x 2 = 8
console.log(2 ** 3) // Result ==> 8

String concatenation using the plus operator

script.js
const firstName = 'Ulrich'
const lastName = 'Ekeu'
console.log(firstName + ' ' + lastName) // Result ==> Ulrich Ekeu

Assignment Operators

The most straight forward assignment operator is just the equal ( = ) sign.

script.js
const result = 12 + 3

The equal sign is actually itself an operator. So in the above code snippet, we have two operators; the equal ( = ) and plus ( + ) operators. In this case, the result variable will be assigned 15, because the plus operator is executed before the assignment operator.

The order in which the operators are executed is based on a couple of rules about operator precedence that I will show you in the next lecture.

Addition assignment operator ( += )

script.js
let ulrichAge = 20
ulrichAge +=  10 // ulrichAge = ulrichAge + 10 = 30console.log(ulrichAge)

What the highlighted line does is simply to increment the value of ulrichAge by 10. That is, instead of writing ulrichAge = ulrichAge + 10, you can directly write ulrichAge += 10. Thus having 30 as the final result being assigned to ulrichAge.

It's basically the same idea with other assignment operators such as the substraction assignment ( -= ), exponentiation assignment (**= ), division assignment ( /= ) etc.

script.js
let u = 12
// Division assignment
console.log(u /= 2) // Result ==> 6

// Substraction assignment
console.log(u -= 3) // Result ==> 3

// Exponentiation assignment
console.log(u **= 2) // Result ==> 9

Increment ( ++ ) and Decrement ( -- ) Operators

All it does is either increment or decrement by one the value of the variable to which it is applied and returns a value. This operator can be used in two ways :

  • Postfix Increment: Meaning the operator is placed after the variable. As an example, let's assume we have the following code snippet :

    script.js
      let valueToIncrement = 12
      let keepCurrentValue = valueToIncrement++

    What happens is that, the increment ( ++ ) operator will increment ( add one ) valueToIncrement ( meaning it's new value will be 13 ) and the value being returned to keepCurrentValue will be the value of valueToIncrement before it was incremented .i.e. 12

  • Prefix Increment: Meaning the operator is placed before the variable. As an example, let's assume we have the following code snippet :

    script.js
      let valueToIncrement = 12
      let keepIncrementedValue = valueToIncrement++

    What happens is that, the increment ( ++ ) operator will increment ( add one ) valueToIncrement ( meaning it's new value will be 13 ) and the value being returned to keepIncrementedValue will be the value of valueToIncrement after it has been incremented .i.e. 13

I talked about postfix and prefix increment, but you have to remember that it's exactly the same idea as postfix and prefix decrement.

Comparison Operators

We use comparison operators to produce boolean values. They will be useful later when we start making decisions with our code based on some conditions. Some examples of comparison operators are, the greater than operator ( > ), the less than operator ( < ), the less than or equal operator ( <= ), the greater than or equal operator ( >= ) etc.

Greater than operator ( > )

The greater than operator checks if the left variable of the operator is greater than the right variable of the operator. If it is greater, it returns true else;, it returns false.

script.js
    let ageJacob = 12
    let ageUlrich = 62
    // Returns false since 12 is not greater than 20
    console.log(ageJacob > ageUlrich) // Result ==> false

From the above code snippet, the left variable is ageJacob and the right variable ageUlrich. Since ageJacob isn't greater than ageUlrich the returned value is false.

Greater than or equal operator ( >= )

The greater than or equal operator checks if the left variable of the operator is greater than or equal to the right variable of the operator. If it is greater or equal, it returns true else;, it returns false.

script.js
    //Let's check if Ulrich has reached the retirement age
    console.log(ageUlrich >= 62) // Result ==> true

From the above code snippet, the left variable is ageUlrich, and the right variable 62. Using the greater than or equal operator, we check to see if Ulrich's age is at least 62. At least, meaning his age is greater than 62 or exactly 62. In this case, it is exactly 62, and the returned value will be true.

To finish the lecture, I want to let you know that you can see the full list of operators used in JavaScript on MDN.