Why can't I use let to reassign a variable?

Why can’t I reassign the value?
It worked when I use var instead of let.

The syntax rules for let are more strict than with var, for one, and for good reason… Why declare a variable more than once? let declares a variable in its current block scope. After that, we can change the value as often as we need.

let sale = true;
sale = false;

let does work like var in terms of declaring variables without defining them.

let a;
var b;

Because loops like for and while consist of their own block of code, let is encouraged to keep the loop data from leaking out. This is useful, and the reason it was incorporated in ES. var is pretty much on the way out as kind of the lame duck in the pond. You’ll still see it a lot, but less and less as the years pass.

Final note, a variable inside a block that is defined by assignment alone becomes a global, automatically.

for (i of [1,2,3,4]) {


We probably don’t want this happening in our program for perhaps less than obvious reasons, but make it a rule to always use a keyword to declare variables. They will be adopted into the scope in which they are declared.


When declaring a variable without defining, can you also declare the data type, as in VBA? That is can I say something like:
let a as boolean
In VBA, this lets you declare variables at the start of a procedure, but not define them until much later, but it will know what type of data to expect.

JavaScript is loosely typed and has no formal way of declaring variables by type. However, ES6 gives us the const declaration keyword to preserve type and value. A primitive will be fixed, both by type and value, but an array or object will be fixed by type only.

const pi = +(Math.PI.toFixed(4))    //  3.1416

Simple enough, but trivial. It just means we cannot reassign pi.

let a = 42
a = 'forty-two'
typeof a    //  'string'

Not only can values be reassigned, but we can assign a new type on the fly.

const arr = []

The type is now fixed, but we can still populate the array and mutate the values at any time.

In JS we can declare variables in advance without defining them.

let x;

The value can be assigned later.

1 Like

I see. So for my own personal sake and preference, if I wanted to declare, but not necessarily define, variables from the start, I could comment out what I want that data type to be. I realize that this would be unnecessary, but I’ve been using VBA so long that I’m used to seeing that. Such as let a; // as boolean

Or, in JS, is it more common/preferred to declare the variable at the first instance of definition? That is wait until I need the variable to “let a = true”?

The scenario will determine when it is necessary to declare a variable. A do..while loop is one example…

let b = Math.floor(Math.random() * 6 + 1)
let a;
do {
    a = Math.floor(Math.random() * 6 + 1)
} while (a !== b);

Not a very good example, though.

Best advice I can offer is to not compare to VBA, but learn this like it is from scratch. Once you know this language you can draw the parallels.

It is common for learners to want to get things right from the start but I suggest put that ambition aside and write whatever comes to mind and get it to work. Treat it like a box of tinker toys and try all the combinations. There will be plenty of time down the road to work on best practices, optimized code, and so on. Right now you just need to get your bearings so set aside the rule book and have fun.