Back to JavaScript
2025-12-085 min read

Assignment expressions (JavaScript)

Learn Assignment expressions (JavaScript) step by step with clear examples and exercises.

Why This Matters

Assignment expressions are a fundamental building block in JavaScript programming, allowing developers to store and manipulate data effectively. Understanding assignment expressions is crucial for writing efficient, bug-free code, especially when dealing with complex data structures or real-world applications. In interviews, recruiters often ask questions about assignment expressions to test your understanding of JavaScript's core concepts. Proper usage can help you avoid common bugs that may arise during development.

Prerequisites

To fully grasp this lesson, you should have a good understanding of the following topics:

  • Basic JavaScript syntax and variables
  • Operators in JavaScript (e.g., arithmetic, comparison, logical)
  • Control structures like if, else, for, and while loops
  • Data types and their properties in JavaScript
  • Functions and function declarations in JavaScript

Core Concept

Assignment Operators

JavaScript offers several assignment operators to assign values to variables or modify existing ones. The most common ones are:

  1. = (simple assignment)
  2. += (addition assignment)
  3. -= (subtraction assignment)
  4. *= (multiplication assignment)
  5. /= (division assignment)
  6. %= (modulus assignment)
  7. **= (exponentiation assignment)**
  8. &= (bitwise AND assignment)
  9. |= (bitwise OR assignment)
  10. ^= (bitwise XOR assignment)
  11. <<= (left shift assignment)
  12. >>= (right shift assignment)
  13. >>>= (signed right shift assignment)

Example: Simple Assignment

let x = 5;
x = x + 3; // x is now 8

Example: Addition Assignment

let a = 10;
a += 20; // a is now 30

Understanding the Order of Precedence

It's essential to understand the order of precedence when using multiple operators in an assignment expression. For example:

let x = 5 + 3 * 2; // x is 13, multiplication has higher precedence than addition

To change the order of operations, you can use parentheses:

let x = (5 + 3) * 2; // x is now 20

Example: Compound Assignment

Compound assignment operators allow you to perform an operation and then assign the result back to the same variable. For example:

let a = 10;
a += 5; // equivalent to a = a + 5, but more concise
console.log(a); // Outputs: 15

Understanding Compound Assignment Order of Precedence

The order of precedence for compound assignment operators is the same as for their non-compound counterparts. For example:

let a = 10;
a += 5 * 2; // equivalent to a = a + (5 * 2), multiplication has higher precedence than addition
console.log(a); // Outputs: 30

Example: Bitwise Assignment Operators

Bitwise operators perform operations on individual bits of numbers. For example, the bitwise AND operator (&) sets each bit to 1 only if both corresponding bits in the operands are set to 1. Here's an example using assignment:

let a = 6; // binary: 0110
let b = 3; // binary: 0011
a &= b; // equivalent to a = a & b, sets each bit to 1 if both bits are set in a and b
console.log(a); // Outputs: 0 (binary: 0000)

Worked Example

Let's create a simple program that calculates the sum, difference, product, and quotient of two numbers using different assignment operators and compound assignment operators.

// Get user input for two numbers
const num1 = prompt("Enter the first number:");
const num2 = prompt("Enter the second number:");

// Calculate sum using simple assignment operator
let sum1 = num1 + num2;
console.log(`Sum using simple assignment: ${sum1}`);

// Calculate sum using addition assignment operator
let sum2 = 0;
sum2 += num1;
sum2 += num2;
console.log(`Sum using addition assignment: ${sum2}`);

// Calculate difference, product, and quotient using other assignment operators
let diff = num1 - num2;
let prod = num1 * num2;
let quot = num1 / num2;

// Display results using simple assignment operator for clarity
let sum3 = 0;
sum3 += diff;
sum3 -= prod;
sum3 *= quot;
console.log(`Sum using difference, product, and quotient: ${sum3}`);

Common Mistakes

Forgetting to Declare Variables

Always declare your variables before using them, as JavaScript does not require you to do so explicitly.

// Incorrect
let x = y + 5; // ReferenceError: y is not defined

// Correct
let y = 3;
let x = y + 5;

Using the Wrong Assignment Operator

Choose the appropriate assignment operator for your needs. For example, if you want to concatenate strings, use the += operator with the + operator instead of simply using the = operator.

// Incorrect
let str1 = "Hello";
str1 = "World" + str1; // str1 is now "WorldHello"

// Correct
let str2 = "Hello";
str2 += " World"; // str2 is now "Hello World"

Assigning to Undefined Variables

If you attempt to assign a value to an undefined variable, JavaScript will create the variable with the assigned value. However, this can lead to unexpected behavior and should be avoided.

// Undefined variable is created with the assigned value of 5
let x;
x = 5;
console.log(x); // Outputs: 5

Practice Questions

  1. Write a program that calculates the sum, difference, product, and quotient of two numbers using different assignment operators and compound assignment operators.
  2. Given an array of numbers, write a function that finds the smallest number and assigns it to a variable called smallest.
  3. Write a function that takes three arguments (a, b, and c) and returns their sum stored in a variable called result. Use different assignment operators for each step of the calculation.
  4. Write a program that calculates the factorial of a number entered by the user using assignment operators and compound assignment operators.
  5. Given two strings, write a function that concatenates them and assigns the result to a variable called result. Use different assignment operators for each step of the calculation.
  6. Write a program that calculates the area and circumference of a circle with radius r entered by the user using assignment operators and compound assignment operators.

FAQ

Q: Can I use multiple assignment operators at once?

A: Yes, you can use multiple assignment operators simultaneously as long as they are separated by commas. For example:

let a = 1, b = 2, c = a + b; // a is 1, b is 2, and c is 3

Q: What happens if I try to assign a value to an undefined variable?

A: If you attempt to assign a value to an undefined variable, JavaScript will create the variable with the assigned value. However, this can lead to unexpected behavior and should be avoided.

// Undefined variable is created with the assigned value of 5
let x;
x = 5;
console.log(x); // Outputs: 5

Q: Can I use compound assignment operators with bitwise operators?

A: No, you cannot use compound assignment operators with bitwise operators directly. However, you can assign the result of a bitwise operation to a variable and then perform a compound assignment on that variable.

let a = 6; // binary: 0110
let b = 3; // binary: 0011
let c = a & b; // equivalent to c = a & b, sets each bit to 1 if both bits are set in a and b
c |= 1; // performs a bitwise OR operation with 1, setting any zero bits to 1
console.log(c); // Outputs: 3 (binary: 0011)
Assignment expressions (JavaScript) | JavaScript | XQA Learn