Basic Assignment with =
In this lesson, you'll learn how the = operator assigns values to variables in JavaScript.
What Assignment Really Means
The equals sign (=) in JavaScript doesn't mean "equals" in the mathematical sense. Instead, it means "store this value in that variable." Think of it like labeling a box: the variable name is the label, and the value is what goes inside.
When you write:
let score = 100;
You're telling JavaScript: "Create a box labeled score and put the number 100 inside it."
Assignment vs Equality
This is crucial: = is assignment, not a question of equality. If you want to check if two things are equal, you use == or === (which you learned earlier).
let x = 5; // Assignment: store 5 in x
x === 5; // Equality: is x equal to 5? (true)
The single = always assigns from right to left. The value on the right side is computed first, then stored in the variable on the left:
let total = 10 + 20; // Calculates 30, then stores it in total
Assignment Is an Action
Assignment happens at a specific moment in your code. You're actively putting a value into a variable:
let temperature = 72;
temperature = 68; // Now temperature holds 68 instead
Key Takeaway: The = operator assigns values to variables (right to left), while === tests equality—don't confuse the two!