Assignment Operators

  • JavaScript’s assignment operators are used to assign variables to different values. Assignment operators each have a symbol known as the operator that is used to perform the operation.

1. Equals : used to assign a variable to a value

  • Operator: =

  • Examples:

    let example = 5;
    let name = "Alex";
    name = example;
    
  • Q : What is the value of the variable named “name”?

    2. Plus-Equals : assigning a variable to its original value plus some specified value

  • Operator: +=

  • Examples:

    x = 5;
    y = 30;
    x += 10;
    x += y;
    y += x;
    
  • Q : What is the value of variable y?

    3. Minus-Equals : assigning a variable to its original value minus some specified value

  • Operator: -=

  • Examples:

    x = 50;
    y = 23;
    x -= 12;
    x -= y;
    y -= x;
    
  • Q : What is the value of variable y?

    4. Times-Equals : assigning a variable to its original value times some specified value

  • Operator: *=

  • Examples:

    x = 13;
    y = 9;
    x *= 3;
    x *= y;
    y *= x;
    
  • Q : What is the value of variable y?

    5. Divide-Equals : assigning a variable to its original value divided by some specified value

  • Operator: /=

  • Examples:

    x = 348;
    y = 24;
    x /= 58;
    y /= x;
    
  • Q : What is the value of variable y?

    6. Modulus-Equals : assigning a variable to its original value modulus some specified value

  • Operator: %=

  • Examples:

    x = 49;
    y = 17;
    x %= 12;
    y %= x;
    
  • Q : What is the value of variable y?

    Exercise 2.2

See the Pen Untitled by LSU DDEM (@lsuddem) on CodePen.