FAQ: Variables - Create a Variable: let

This community-built FAQ covers the “Create a Variable: let” exercise from the lesson "Variables ".

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development

Introduction To JavaScript

FAQs on the exercise Create a Variable: let

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

4 posts were split to a new topic: Why don’t we need quotations for numbers or booleans?

25 posts were split to a new topic: What are the differences between var and let? Which should I use?

8 posts were split to a new topic: Why would we want to reassign a new value to a variable?

What’s the purpose or use case for NOT assigning a value to a variable and having the variable be undefined?

let price;
console.log(price); // Output: undefined

There’s nothing in the Codecademy lesson on use case or purpose. Thanks.

1 Like

lets say i want to prompt the user for input:

let result;
while (true){
    result = promt("give me a number:");
    if (valid(result)){ break; }
}

valid() represents a function which would validate result (to see if result is a number, i was too lazy to implement that, given its not relevant to the question), given let is block scoped, and i want to use result after the loop, i will first need to define the variable outside the block scope of the loop.

maybe not a greatest example in the history of examples, but you need to know its possible to define variable without value, in case you might ever need it

So this is the soluton but I don’t understand what it is asking in the instructions, can someone please explain this?

Create a let variable called changeMe and set it equal to the boolean true .

I know a boolean is a data type but what do I do with that ?

let changeMe = true; // what is this?

changeMe = false; // why does this say false when it's stupposed to be true?

console.log(changeMe);

I got the

let price;
console.log(price); // Output: undefined
price = 350;
console.log(price); // Output: 350

example but what is this other solution that looks nothing like this …

a simple variable which you give a Boolean value.

you re-assign the variable. Now the value is false.

the example and the task are similar. If they would identical, a lot of people would just be copy pasting and not learning much.

I’m confused how one example uses different names of foods and this example uses true/false statements. I don’t see how those are similar.

This exercise focuses on variable assignment and re-assignment. What data type you (re)assign to the variable is less relevant.

I still don’t understand why it wants me to use true and false instead of something tangible like food. True or false is … some other type of data? I should know this but I can’t remember it

true and false are Booleans, given booleans is one the primary data types and essential in conditions (huge part of programming), you should know :wink:

you can always consult earlier lessons or documentation when you need to. There is no shame in that, in fact, its a good thing :slight_smile:

1 Like

I do remember the word boolean, just no idea what it is…I don’t know how I got thru the exercise for that.

I have some of the methods saved (i’ll never remember those) but I don’t know any other documentation. I’ve reviewed each lesson at least 4-5 times and done them many times over and I feel like I never remember any of it very long or maybe just how to finish the exercise.

what is the difference between
let changeMe;
changeMe = true;

and

let changeMe = true;

in the first example, you define/declare a variable, you don’t give it a value, so the variable will now hold undefined. Then later you update the variable.

in the second example, you give the variable a value

There are cases where this might be useful, but better to save that for later. Otherwise things get too complex right away

Why should I use let instead of using var?

let is block-scoped, so when using var:

for (var i = 0; i < 5; i++){

}
 console.log(i);

the variable i leaks outside its intended scope. with a block scope, we don’t have this problem

1 Like

so does it means that 1st value of let is now considered as obsolete? why not just replace the value rather than reassigning new one? as of this stage of learning, it’s quite weird to me…is it because it maybe hard to locate in the future so your declaring a new value?

In some cases, replacing might be possible. But there are other times where re-assigning is the way to go.

Sometimes, a concept is taught but might not be clear what the advantages are. That is okay.

lets say you want to prompt the user input, and the input needs to be a number. I don’t want keep all invalid inputs around. So then re-assigning is the way to go

If I assign a new value to the variable using let, will the first value be overridden or it will still be printed using console object?