I'm learning Javascript and I've gotten to conditionals. When I declare a variable 'sale' and assign it a value of true, my if statement runs, but if I reassign the value of sale to false, the code does not execute. Why is this happening?
let sale = true;
if (sale){
console.log('Time to buy!');
}
The above code executes, but the following code doesn't and I don't understand why. I'm new to coding.
let sale = false;
if (sale = false){
console.log('Enough inventory');
}
The "=" statement in most programming languages is an assignment operator, it assigns values to certain variable names. What you are looking for is a "==" this checks if something is equal or not.
let sale = false;
if (sale == false){
console.log('Enough inventory');
}