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

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

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!

I have a question about using let as a variable. In one example, the value assigned to the variable after the = sign is put in quotes, and in other examples (the Boolean false and the number), no quotes were used. Is there an easy way to remember when quotes should be used and when they shouldn’t?

The quotes is used only with string argument, boolean and number don’t need it.
If you put quotes around a number, or boolean, it will be treated as a String

1 Like

I ran both examples using var instead of let keyword in my browser console and both outputs where exactly the same as if using let (Burrito and 350). Based on this result, please help me better understand the differences.

8 Likes

I would like to know the difference as well.

So do ı … What’s difference?

here is a good explanation:

https://stackoverflow.com/questions/762011/whats-the-difference-between-using-let-and-var-to-declare-a-variable-in-jav

which goes into more depth, the simplified version is that let is block scoped:

if (true){
   let letVariable = 'hello world';
   var varVariable = 'hello world';
   console.log(varVariable);
   console.log(letVariable);
}
console.log(varVariable);
console.log(letVariable); // syntax error or undefined

this is because the variable declared with let only exist within the if block (between the {})

2 Likes

Hi Codecademy learners. This is with respect to the exercise “difference between var and let” - the lesson says that we can only reassign variables that were declared using “let”. But I ran the same example example using “var” and it still works. Why is that? What is the little difference that I’m missing? I found a link to one of the codecademy posts that says let is block-scoped and var is not. So what happens if I need to change a global variable (doesn’t that global variable need be declared as var in that case?)

var meal = 'Enchiladas';
console.log(meal); // Output: Enchiladas
meal = 'Burrito';
console.log(meal); // Output: Burrito

just like any piece of software, programming languages get updates. The latest major update of javascript was in 2015 (version 6, known as es6 or emcascript 2015). Before 2015 we then obviously had version 5 (es5)

let and const where introduced in es6, so when the lesson says we can only reassign variable declared with let, its comparing with const, not the older var (var is es5, or maybe older)

no, we can declare global variable with let, then the scope is the same (global). But you need to be aware of the block scope when that comes into play (when variable are declared in a block). Global variable declared with let are not added to the window object (see the stackoverflow question in my previous answer in this topic)

Var has not become completely useless, but the problem is that when you just start to learn JS, these concepts (like the window object), are difficult to grasp, because you don’t have much experience with them. So its okay if you don’t comprehend it all yet, that will come with time

3 Likes

Thank you! It helped.

The difference is scoping. var is scoped to the nearest function block and let is scoped to the nearest enclosing block, which can be smaller than a function block. Both are global if outside any block.

Also, variables declared with let are not accessible before they are declared in their enclosing block. As seen in the demo, this will throw a ReferenceError exception.

Global:

They are very similar when used like this outside a function block.

let me = 'go';  // globally scoped
var i = 'able'; // globally scoped

However, global variables defined with let will not be added as properties on the global window object like those defined with var .

console.log(window.me); // undefined
console.log(window.i); // 'able'

Function:

They are identical when used like this in a function block.

function ingWithinEstablishedParameters() {
    let terOfRecommendation = 'awesome worker!'; //function block scoped
    var sityCheerleading = 'go!'; //function block scoped
}

Block:

Here is the difference. let is only visible in the for() loop and var is visible to the whole function.

function allyIlliterate() {
    //tuce is *not* visible out here

    for( let tuce = 0; tuce < 5; tuce++ ) {
        //tuce is only visible in here (and in the for() parentheses)
        //and there is a separate tuce variable for each iteration of the loop
    }

    //tuce is *not* visible out here
}

function byE40() {
    //nish *is* visible out here

    for( var nish = 0; nish < 5; nish++ ) {
        //nish is visible to the whole function
    }

    //nish *is* visible out here
}

Redeclaration:

Assuming strict mode, var will let you re-declare the same variable in the same scope. On the other hand, let will not:

'use strict';
let me = 'foo';
let me = 'bar'; // SyntaxError: Identifier 'me' has already been declared
'use strict';
var me = 'foo';
var me = 'bar'; // No problem, `me` is replaced.
3 Likes

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?

yes, let, var and 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.

What is the difference between var and let keywords like when to use var or when to use let ?

var has function scope, while let has block scope:

function example():
   if (true) {
      var functionScope = true;
      let blockScope = true;
   }
   console.log(functionScope);
   console.log(blockScope); // this line gives an error
}

the reason why blockScope gives an error, is because it only exists within the if block. functionScope is also defined within the if block, but its scope rules are different

you should use let mostly, given its newer and block scope leads to less scope pollution and bugs. For now, assume this is true, you will learn more about it once you gain more experience.

Thank you @matrenitski. very well explained.

Hello, I have a question.

Why after changing the value of the variable, it still prints //True

Edit**

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.)