Bird
Raised Fist0
Javascriptprogramming~5 mins

Assignment operators in Javascript - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What does the = operator do in JavaScript?
The = operator assigns the value on the right to the variable on the left. For example, x = 5 sets the variable x to 5.
Click to reveal answer
beginner
What is the difference between = and += operators?
= assigns a value directly, while += adds the right value to the left variable and then assigns the result back. For example, x += 3 means x = x + 3.
Click to reveal answer
beginner
List three compound assignment operators in JavaScript besides +=.
Three common compound assignment operators are:
1. -= (subtract and assign)
2. *= (multiply and assign)
3. /= (divide and assign)
Click to reveal answer
intermediate
What happens if you use x ||= y in JavaScript?
The ||= operator assigns y to x only if x is falsy (like null, undefined, 0, or ''). It's a shortcut for x || (x = y).
Click to reveal answer
intermediate
Explain the difference between &= and ^= operators.
&= performs a bitwise AND between the variable and the right value, then assigns the result back.
^= performs a bitwise XOR (exclusive OR) between the variable and the right value, then assigns the result back.
Click to reveal answer
What does the operator += do in JavaScript?
AAdds the right value to the left variable and assigns the result to the left variable
BAssigns the right value to the left variable without change
CSubtracts the right value from the left variable
DMultiplies the left variable by the right value
Which operator assigns a value only if the left variable is falsy?
A+=
B||=
C&&=
D*=
What does x &= y do?
APerforms bitwise OR between x and y, then assigns to x
BAdds x and y, then assigns to x
CAssigns y to x if x is falsy
DPerforms bitwise AND between x and y, then assigns to x
Which operator multiplies and assigns in one step?
A*=
B/=
C+=
D-=
What is the result of let x = 5; x ||= 10;?
Ax is 10
Bx is 15
Cx is 5
DError
Explain how compound assignment operators work in JavaScript with examples.
Think about how you can shorten 'x = x + 3' using a compound operator.
You got /3 concepts.
    Describe the purpose and behavior of logical assignment operators like ||= and &&=.
    Consider how these operators help assign values only under certain conditions.
    You got /3 concepts.

      Practice

      (1/5)
      1. What does the assignment operator += do in JavaScript?
      easy
      A. Adds the right value to the left variable and updates the variable
      B. Subtracts the right value from the left variable
      C. Assigns the right value to the left variable without change
      D. Multiplies the left variable by the right value

      Solution

      1. Step 1: Understand the += operator

        The += operator adds the value on the right to the variable on the left.
      2. Step 2: Effect on the variable

        It updates the left variable with the new sum, combining addition and assignment.
      3. Final Answer:

        Adds the right value to the left variable and updates the variable -> Option A
      4. Quick Check:

        x += y means x = x + y [OK]
      Hint: Think: add then save back to the same variable [OK]
      Common Mistakes:
      • Confusing += with simple assignment =
      • Thinking it subtracts or multiplies
      • Believing it only adds without updating
      2. Which of the following is the correct syntax to multiply a variable num by 5 and update it using an assignment operator?
      easy
      A. num =* 5;
      B. num = *5;
      C. num *= 5;
      D. num = 5*;

      Solution

      1. Step 1: Identify the correct multiplication assignment syntax

        The correct operator to multiply and assign is *=, placed between variable and value.
      2. Step 2: Check each option

        num *= 5; uses num *= 5; which is valid syntax. Others have incorrect operator placement or invalid syntax.
      3. Final Answer:

        num *= 5; -> Option C
      4. Quick Check:

        Use *= for multiply and assign [OK]
      Hint: Remember operator goes between variable and value, no spaces inside [OK]
      Common Mistakes:
      • Writing =* instead of *=
      • Adding spaces inside operator
      • Using invalid symbols like 5*;
      3. What will be the output of the following code?
      let x = 10;
      x -= 3;
      console.log(x);
      medium
      A. 13
      B. 7
      C. 3
      D. 10

      Solution

      1. Step 1: Understand the initial value and operation

        Variable x starts at 10. The operator -= subtracts 3 from x.
      2. Step 2: Calculate the new value

        10 - 3 = 7, so x becomes 7.
      3. Final Answer:

        7 -> Option B
      4. Quick Check:

        x -= 3 means x = x - 3 [OK]
      Hint: Subtract then save result back to variable [OK]
      Common Mistakes:
      • Adding instead of subtracting
      • Printing old value before update
      • Confusing -= with =-
      4. Find the error in this code snippet:
      let count = 5;
      count +== 2;
      console.log(count);
      medium
      A. Console.log should be console.print
      B. Variable count is not declared
      C. Missing semicolon after declaration
      D. The operator +== is invalid syntax

      Solution

      1. Step 1: Identify the operator used

        The code uses +== which is not a valid assignment operator in JavaScript.
      2. Step 2: Correct operator usage

        The correct operator to add and assign is +=. So it should be count += 2;.
      3. Final Answer:

        The operator +== is invalid syntax -> Option D
      4. Quick Check:

        Use += not +== for addition assignment [OK]
      Hint: Assignment operators have one = sign only [OK]
      Common Mistakes:
      • Using double equals with + (like +==)
      • Confusing console.log with other functions
      • Ignoring semicolon errors (usually optional)
      5. Given the code below, what will be the final value of result?
      let result = 2;
      result *= 3;
      result += 4;
      result /= 2;
      hard
      A. 5
      B. 7
      C. 8
      D. 10

      Solution

      1. Step 1: Multiply result by 3

        Starting with result = 2, after result *= 3, result becomes 6.
      2. Step 2: Add 4 to result

        After result += 4, result becomes 6 + 4 = 10.
      3. Step 3: Divide result by 2

        After result /= 2, result becomes 10 / 2 = 5.
      4. Final Answer:

        5 -> Option A
      5. Quick Check:

        2 * 3 + 4 then / 2 = 5 [OK]
      Hint: Follow each operator step-by-step to update variable [OK]
      Common Mistakes:
      • Doing operations in wrong order
      • Forgetting to update variable after each step
      • Mixing up division and multiplication
      Recommended

      Ready to prove it?

      Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

      10 questions~5 mineasy hard