Why would you need the ability to change the value(?) of a variable after the fact?
changing a variable value is so common, without this ability, programming would be a lot more difficult.
lets say you capture user input in a variable:
var userInput = promt('give me a word: ')
if the user enters multiple words, you can prompt the user again and store the result in the same variable. If you had to store the user input in a different variable each time, that would become tedious if you get wrong input multiple times
there are countless example why re-assign variable is very useful, you will see
let meal = ‘Enchiladas’;
console.log(meal); // Output: Enchiladas
meal = ‘Burrito’;
console.log(meal); // Output: Burrito
From the example in the exercise, the 3rd line does not use the keyword let to reassign a different value. Is this a normal practice?
const are only used when defining a variable (for scope among other things), once the variable is defined we can just assign it other values.
Hello, I have a question.
Why after changing the value of the variable, it still prints //True
I just found out that I forgot to console.log() the variable… Sorry!
But now I have another question!
Every single time a print changeMe will be false after this line?
I’m sure this question will be answered, however I’m trying to conceptualize this exercise, I am wondering why would we need to reassign a new value to a variable? Shouldn’t we simply declare a new variable? Wouldn’t it get confusing as the code continues (like re-writing a class in css that gets redefined later on in the style sheet.)
yes, the variable will now hold false until you change it again
you can’t compare variable with css.
re-assigning and declaring variables are both concepts you to know for programming.
lets say you want to prompt the user for valid input, it might take the users 5 try to get it right, then i want a single variable (the user input one thing), not 5 variable of which 4 are completely useless.
@acgatorfan There are many reasons to reassign variables. Imagine you have a quiz website.
when a user starts the quiz, their initial score would be zero, as they answer more questions the score increases. So at the end of the quiz their score could be 10. so we need to be able to continuously reassign the variable score to keep track of the score.
This also applies in games, your player health,lives,ammunition etc
Thank you for that I was stuck before thinking that let reassigned the value instead of showing that the value COULD be changed
let changeMe = true
let changeMe = false
let changeMe = true
changeMe = false
This is the most helpful answer I have seen! I have been finding it difficult to conceptualize why this is needed. However, since it’s one of the first things I am learning with JS I figured it would be a super important aspect of the language to understand haha. Thank you so much!
It is necessary to be able to store values according to the need or the flow of a solution. This characteristic allows deions to be determined with respect to an entered value.
You have applied console.log(changeMe) before defining = false;
If you apply console.log(changeMe) after that 3rd line. It probably will appear as false.
Imagine you wanted to create a website where users have usernames. The user might want to change their username at some point in time. When they change it, the new value will simply replace the previous value in the variable.
This is just one of many use cases
Not sure I agree, variable only exists/persist while the program is running
users for a website are stored in database, which persist on disk.
I agree that most website store usernames in a database, however not all do.
For example, simple web browser games that are built to run without the need for a server or database.
The issue is on my part, I should have used a better example.