About Lesson
Assignment Operators
Used to assign values to variables:
- = (Assign): x = 5
- += (Add and assign): x += 5 (same as x = x + 5)
- -= (Subtract and assign): x -= 5
Example:
JavaScript
// Declare a variable and assign an initial value
let x = 5;
console.log("Initial value of x: " + x); // 5
// Using = (Assign)
x = 10;
console.log("After using =, x is: " + x); // 10
// Using += (Add and assign)
x += 5; // Same as x = x + 5
console.log("After using +=, x is: " + x); // 15
// Using -= (Subtract and assign)
x -= 3; // Same as x = x - 3
console.log("After using -=, x is: " + x); // 12